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

    Unhappy Sorting and Printing file

    Hi all , :confused:

    I need to sort a text file and then output it again as another file which has the sorted data .

    my input file looks like this which is a tab delimited data

    name 7 name 5
    area 9 area 7 area 10
    location 8 location 6 location 9

    i would like to sort the data only within the lines and print it and save it to another file.
    The output is supposed to be as follows

    name 5 name 7
    area 7 area 8 area 10
    location 6 location 8 location 9


    my code looks like this



    import java.io.*;
    import java.util.*;

    public class ReadFile {

    public static void main(String args[]) {
    try {
    // Open the file

    FileInputStream fstream = new FileInputStream("List.txt");

    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;


    //Read File Line By Line

    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);
    // Write File
    FileWriter writer = new FileWriter("output.txt");
    for (String cur : partlist) {
    writer.write(cur + "\n");
    }
    writer.close();

    }


    } catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }


    the problem is that my output file contains only the last line of the output that is printed , i don't have my entire data in it. Could somebody please help me with this. I don't know what I am doing wrong here. :confused:
    Last edited by spry.chipper; 12-13-2009 at 12:46 PM.

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    everytime your while loop is running, your output text get overwrited.
    the line:
    FileWriter writer = new FileWriter("output.txt");

    shoud be outside the while-loop, next to this line:
    FileInputStream fstream = new FileInputStream("List.txt");

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

    Default

    Thanks for the reply, i have modified the loop as suggested and now it works .

    I have another problem though

    my

    // Sort and Print

    Collections.sort(partlist);
    System.out.println(partlist);
    // Write File

    for (String cur : partlist) {
    writer.write(cur + "\t");

    produces the following result

    [name 5, name 7]
    [area 7, area 8, area 10]
    [location 6 ,location 8, location 9]

    but my output file "output.txt" contains the text in the following format

    name 5 name 7 area 7 area 8 area 10 location 6 location 8 location 9

    basically all the data is in one line

    how do i make the writer to output the file line by line as below ?


    name 5 name 7
    area 7 area 8 area 10
    location 6 location 8 location 9

    I m sorry that i may sound stupid, but this is my first time in learning a programming language, any help i can get is gladly welcome.

    thanks in advance .

  4. #4
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    You can wrap the FileWriter with a PrintWriter, and use println

    FileWriter writer = new FileWriter("output.txt");
    PrintWriter w = new PrintWriter(writer);

    for (String cur : partlist)
    {
    w.write(cur+" ");

    }
    w.println();

Similar Threads

  1. Printing text file
    By amitcs1001 in forum NetBeans
    Replies: 2
    Last Post: 11-30-2009, 03:17 AM
  2. sorting data in txt file
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 04:02 AM
  3. Log4j not printing out file and line number
    By devin in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2009, 04:16 PM
  4. Job name when printing file
    By pjmorce in forum Advanced Java
    Replies: 3
    Last Post: 12-23-2008, 02:55 AM
  5. Sorting xml file
    By keioGirl in forum Advanced Java
    Replies: 4
    Last Post: 12-12-2008, 06:14 PM

Tags for this Thread

Posting Permissions

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