This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes TreeSet - Object input and output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "TreeSet - Object input and output " Watch "TreeSet - Object input and output " New topic

TreeSet - Object input and output

Linda Burritt

Joined: Dec 08, 2002
Posts: 1
I have two problems I cannot seem to solve.
First on is I need to beable to write to a file and then read that data back. I am able to write to the file but cannot read the information back.
The Second problem is that I need to print my sets in reverse order. I thought I knew how to do it but nothing seems to happen when I run the command. I have attached my code for your review.import java.util.*;

public class TestSort
implements Serializable {
static int n;
static int nn;

public TestSort() {
public void set1() {
Integer[] set1 = new Integer[n];
TreeSet tree1 = new TreeSet();
/** generates random numbers for set # 1*/
for (int i = 0; i < set1.length; i++) {
set1[i] = new Integer( (int) (Math.random() * nn));
/** adding set1 to tree1*/
System.out.println("\nset 1: \t");
/** Should print in reverse !!! Doesn't work*/
// // }
// public void printSet2(){ // This works prints out set 2
Integer[] set2 = new Integer[n];
TreeSet tree2 = new TreeSet();
TreeSet tree3 = new TreeSet();
TreeSet tree4 = new TreeSet();
/** generates random numbers for set # 2 */
for (int i = 0; i < set2.length; i++) {
set2[i] = new Integer( (int) (Math.random() * nn));
// adds set2 into tree2
System.out.println("\nSet 2: ");
System.out.println("\nUnion: " + tree1);
Set intersection = new TreeSet(tree2);
//System.out.println("tree2" + tree2);
System.out.println("Intersection: " + intersection);
try {
// create file and output object streams
FileOutputStream outputItemFile = new FileOutputStream("SortedSet.txt");
ObjectOutputStream out = new ObjectOutputStream(outputItemFile);
out.writeObject("\nUnion: " + tree1);
out.writeObject("\nIntersection: " + intersection);
out.writeObject("\nSet 1: " + tree3);
out.writeObject("\nSet 2: " + tree2);
catch (Exception error) {
System.err.println("Error opening file 1");
try {
System.out.println("This is the begining of the input");
FileInputStream inputItemFile = new FileInputStream("SortedSet.txt");
System.out.println("This is after fileinputstream");
ObjectInputStream in = new ObjectInputStream(inputItemFile);
System.out.println("This is after the objectinputstream");
tree1 = (TreeSet) in.readObject();
intersection = (TreeSet) in.readObject();
tree3 = (TreeSet) in.readObject();
tree2 = (TreeSet) in.readObject();

System.out.println("This is ofter the set object");
System.out.println("This is before the for loop in the input");
for (int t = 0; t < t++; ) {
System.out.println("This is after the for loop");
System.out.println("Union: " + tree1);
System.out.println("Intersection: " + intersection);
System.out.println("Set1: " + tree3);
System.out.println("Set2: " + tree2);
System.out.println("This is the end of the input");
catch (Exception error) {
System.err.println("Error opening file 2");
} //
This code below is my main method.

public class TestSortDriver {
public TestSortDriver() {
public static void main(String[] args) {
{TestSort test = new TestSort();}
/** Iterator for set 1*/
public static void printSet( SortedSet set1 )
Iterator iterator = set1.iterator();
while ( iterator.hasNext() )
System.out.print((Integer) + "," );
/** Iterator for set 2*/
public static void printSet2( SortedSet set2 )
Iterator iterator = set2.iterator();
while ( iterator.hasNext() )
System.out.print( + "," );
/** prompts the users*/
public static void promptUsers()
/** Asks the user to enter the total of number in a set (array)*/
TestSort.n = promptForInt("\nEnter the array: \t");
/**Asks the user to enter the number range they want in a set*/
TestSort.nn = promptForInt("\nEnter the number range for the sets:\t");
public static int promptForInt(String prompt)
String s = "";
BufferedReader ds = new BufferedReader(new InputStreamReader(;
try{s = ds.readLine();
catch (IOException e){System.out.println(e);
return Integer.parseInt(s);

Linda Burritt
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Linda,
Your first problem seems easy to solve if you look carefully at your code. Instead of writing your objects with comments:

Just write the objects:

Your second question is (if I understood well): how to use a TreeSet object with a reverse ordering?
To enable reverse sorting in a TreeSet, build your object with the following construction code:

Now set2 will use inverse natural ordering when adding items.
If you prefer to reverse a existing TreeSet:

I agree. Here's the link:
subject: TreeSet - Object input and output
It's not a secret anymore!