Reordering within a text file
Hi all,
I have a text file in the following format,
Part 1 Part 2
p2_f1 p2_f2 p2_f3 p2_f4 p2_f5 p2_f6
p1_f1 p1_f2 p1_f3 p1_f4 p1_f5 p1_f6
I would like to sort the lines starting the second according to the data in the first line,
for example if the first word in the first line is "Part 1" then the second line should be
p1_f1 p1_f2 p1_f3 p1_f4 p1_f5 p1_f6
if the second word in the first Line is "Part 2" then the third line should be
p2_f1 p2_f2 p2_f3 p2_f4 p2_f5 p2_f6
I have the following code to read the file and put it into an arraylist, any ideas on how to proceed from here? I m totally a newbie with java , any help would be greately appreciated.
Code:
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Reorder {
public static void main(String args[]) throws IOException {
FileInputStream fstream = null;
try {
fstream = new FileInputStream("E:\\Documents\\reorder.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
ArrayList<String> partlist = new ArrayList<String>();
// Intialize tokenizer
String s = strLine;
StringTokenizer tokenizer = new StringTokenizer(s, "\t");
while (tokenizer.hasMoreTokens()) {
// add data to collection
partlist.add(tokenizer.nextToken());
}
// Sort and Print
Collections.sort(partlist);
System.out.println(partlist);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Reorder.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fstream.close();
} catch (IOException ex) {
Logger.getLogger(Reorder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}