Results 1 to 3 of 3
  1. #1
    spry.chipper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Unhappy 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);
                }
            }
    
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Come on now!

    You've been told already about the sins of cross-posting without notification. What part about this do you not get?!

  3. #3
    spry.chipper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. Replies: 8
    Last Post: 11-06-2009, 07:52 AM
  3. Cutting parts of an image and reordering them
    By pedjacar in forum Java 2D
    Replies: 2
    Last Post: 10-16-2008, 10:46 AM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •