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?