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?

     
 
swp3h6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.