• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Sorting entries in a text file

my problem goes like this:

I have a text file filled with entries that each have a category name and delimiter in front of them, kindof like the following:

Category1::blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

Category2::blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

Category1::blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

and so forth....


I'd like to sort these entries by their categories and print the results to a new text file like this:

Category1

     blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

Category 2

     blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah blahblahblahblahblah blahblahblahblahblah

====================================

what would be the easiest way to do this?

     
 
0
swp3h6
Asked:
swp3h6
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
CEHJCommented:
You need to read them into a collection, sort them and then rewrite them. You can use this class and call Collection.sort on it directly:

package net.proteanit.util;

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

/**
 *  A utility class the allows a text file to be
 *  treated as a collection of Strings
 *
 * @author     CEHJ
 * @created    29 February 2004
 */
public class StringList extends ArrayList {

  /**
   *Constructor for the StringList object
   */
  public StringList() {
    super();
  }

  public void read(InputStream in) {
    read(new InputStreamReader(in));
  }


  public void read(Reader r) {
    String line = null;
    BufferedReader in = null;
    try {
      in = new BufferedReader(r);
      while ((line = in.readLine()) != null) {
        add(line);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  /**
   *  Constructor for the StringList object
   *
   * @param  fileName  The file to open
   */
  public void read(String fileName) {
    try {
      read(new FileReader(fileName));
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }

  /**
   *  Save the String to named file
   *
   * @param  fileName  The name of the file to save to
   */
  public void save(String fileName) {
    PrintWriter out = null;
    try {
      out = new PrintWriter(new FileOutputStream(fileName), true);
      for (int i = 0; i < size(); i++) {
        out.println((String) get(i));
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (out != null) {
        out.close();
      }
    }
  }

}
0
 
objectsCommented:
is this homework?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Use this for reading from a file (store the contents in a String-array or an array-list):

http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html

Sort the list using Collections.sort ().

Use this for writing to another file: http://www.javaalmanac.com/egs/java.io/WriteToFile.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
swp3h6Author Commented:
okay, i'll try these things out.  

And no its not homework, just work.
0
 
CEHJCommented:
StringList sl = new StringList();
sl.read("x.txt");
Collections.sort(sl);
sl.save("sorted-x.txt");
0
 
objectsCommented:
try something like the following. Let me know if you have any questions:

public class Sorter
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader in = new BufferedReader(new FileReader("file.txt"));
      List lines = new ArrayList();
      String s;
      while ((s = in.readLine()) != null) {
            lines.add(s);
      }
      in.close();
      Collections.sort(lines);
      PrintWriter out = new PrintWriter(new FileWriter("results.txt"));
      Iterator i = lines.iterator();
      String cat = null;
      while (i.hasNext()) {
         s = (String) i.next();
         String[] t = s.split("::");
         if (cat==null || !cat.equals(t[0])) {
          cat = t[0];
          System.out.println(cat);
          System.out.println();
        }
        System.out.println(t[1]);
          System.out.println();
      }
      out.close();
   }
}
0
 
swp3h6Author Commented:
Thanks for all the input everyone, but I never got any kind of Collections thing to work for me.  I solved the problem by using a 2 dimentional array to sort the entries by category.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
That is also possible, but using Collections, you prevent writing a lot of code because you wil use the in-built API :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now