Results 1 to 3 of 3
Thread: Reordering within a text file
- 12-13-2009, 05:11 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
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.
Java 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); } } } }
-
Come on now!
You've been told already about the sins of cross-posting without notification. What part about this do you not get?!
- 12-13-2009, 05:51 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Insert text records from text file into a DB
By nicedad in forum JDBCReplies: 8Last Post: 11-06-2009, 06:52 AM -
Cutting parts of an image and reordering them
By pedjacar in forum Java 2DReplies: 2Last Post: 10-16-2008, 09:46 AM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks