Solved

# Sorting entries in a text file

Posted on 2006-03-19
169 Views
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
Question by:swp3h6

LVL 86

Accepted Solution

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

}

String line = null;
try {
while ((line = in.readLine()) != null) {
}
}
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
*/
try {
}
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

LVL 92

Expert Comment

is this homework?
0

LVL 30

Assisted Solution

Use this for reading from a file (store the contents in a String-array or an array-list):

Sort the list using Collections.sort ().

Use this for writing to another file: http://www.javaalmanac.com/egs/java.io/WriteToFile.html
0

Author Comment

okay, i'll try these things out.

And no its not homework, just work.
0

LVL 86

Expert Comment

StringList sl = new StringList();
Collections.sort(sl);
sl.save("sorted-x.txt");
0

LVL 92

Assisted Solution

try something like the following. Let me know if you have any questions:

public class Sorter
{
public static void main(String[] args) throws IOException
{
List lines = new ArrayList();
String s;
while ((s = in.readLine()) != null) {
}
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

Author Comment

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

LVL 30

Expert Comment

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

### Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (CODâ€¦
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correctâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.