1

I'm looking for a small code snippet that will find how many certain numbers cells in first column and cut 1/4 of rows (all line) to another file. So for example I have in a file following:

dataset.txt:

0 139 0.22 0.34 0.48 0.50 0.42 0.29 0.39

0 140 0.44 0.35 0.48 0.50 0.44 0.52 0.59

0 141 0.27 0.42 0.48 0.50 0.37 0.38 0.43

0 142 0.16 0.43 0.48 0.50 0.54 0.27 0.37

1 143 0.06 0.61 0.48 0.50 0.49 0.92 0.37

The code will find how many 0 in first column and take 1/4 rows to another file and I get file like this:

myFile.txt:

0 139 0.22 0.34 0.48 0.50 0.42 0.29 0.39

my code:

public static void main(String[] args) {
    String in = "File.txt";
    String out = "File_new.txt";
    convert(in, out);
}

private static void convert(String inPath, String outPath) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(inPath));
        BufferedWriter writer = new BufferedWriter(new FileWriter(outPath));
        String line;
        while((line = reader.readLine()) != null) {

         String[] aaa = line.split(" ");
         String newLine = "";                                  
         if(aaa[0].equals("0"))
         for(int i =0; i < aaa.length; i++)
         newLine += aaa[i]+' ';
         newLine += '\n';                                       
         writer.write(newLine);  
            // ?

        }
        reader.close();
        writer.close();
    } catch(Exception exc) {
        System.out.print(exc.getMessage());
    }
}

How should the implementation inside while look like? I made this : this function writing rows to a new file rows that start 0 , and now how can i delete this line after copy to new file?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Bartek
  • 13
  • 6
  • 3
    SO doesnt write code. Please let us know your attempt. – SMA Apr 20 '15 at 15:55
  • You only need to read a line watch if start for 0 or 1, and then copy to a new file, is easy if you know java. If you dont know java the best solution is to read any manual in net. Then copy your code and people will help you. – Distopic Apr 20 '15 at 15:59
  • Now you have the file, the next step is to evaluate a line, for example try with this : http://stackoverflow.com/questions/16100175/store-text-file-content-line-by-line-into-array – Distopic Apr 20 '15 at 16:22

3 Answers3

0

First thing, you only need the code to execute if the first character is a 0, if I understand your question correctly. I would do something like:

while(has next character) {
     while (first character is != 0) {
         writer.print(...);
         ...;
     }
}

That is just the pseudo-code for what you are trying to accomplish. Please try to write this yourself, and if you can't, make an edit. Hope that was helpful!

EDIT**

the code to do this is listed below:

while(reader.hasNext()) {
     while (reader.nextDouble() == 0.0) {
         writer.print(reader.nextLine());
     }
}
oakTree
  • 159
  • 11
0

while((line = reader.readLine()) != null) {

     String[] aaa = line.split(" ");
     String newLine = "";                                  
     if(aaa[0].equals("0"))
     for(int i =0; i < aaa.length; i++)
     newLine += aaa[i]+' ';
     newLine += '\n';                                       
     writer.write(newLine);  


    }
Bartek
  • 13
  • 6
-1

Not sure if I understand your question very well, but I'd do the following:

    //1. Based on my file path
    String filePath = "c:/mydatafile.txt";
    //2. I will reference the file path using "File" class
    File myTxtFile = new File(filePath);
    //3. Then BufferedReader to read the file
    BufferedReader bufferedReader = new BufferedReader(new FileReader(myTxtFile));
    //4. Start reading the file
    //You would like to have a variable to evaluate each line in your file
    String line = null;
    //StringBuilder to take the characters I want and save them to this
    StringBuilder appender = new StringBuilder();
    //Then loop...
    while ((line = bufferedReader.readLine()) != null) {
        //Evaluate the current line being read by the bufferedReader
        //You can use whatever you feel comfortable here: regex (matches), tokenizer, startsWith... but you need to think this
        if (condition) {
            appender.append(myVal);
        }
    }

    //Close the reader
    bufferedReader.close();

    //At the end I'll have my stringbuilder "appender" with the information I want and the only thing I would do is:
    String targetFile = "c:/mynewfile.txt";
    File myNewFile = new File(targetFile);

    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(myNewFile));
    bufferedWriter.write(appender.toString());
    //Close the writer
    bufferedWriter.close();

Maybe this could be helpful for what you look for. Happy coding!

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51