File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I'm stuck!

 
Allan Peak
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a Java program for my own use. However, I'm stuck on two things.
1. How do I read/write my user-defined classes to a file?
2. Can I sort my user-defined class on one or more key fields?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan -
Throw us a little sample code, and I'm sure you'll get a lot of feedback!
-Bert
 
Allan Peak
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm mostly in the planning stages, and don't have much useable code yet, but here's what my class currently looks like:
public class Check
{
String cYear;
String cMonth;
String cDay;
String cNum;
String payee;
String memo;
String category;
Boolean balanced;
float amt;
}
I'd like to sort on the date fields & the cNum field.
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To write an object, you can use writeObject( ), which is a member of ObjectOutputStream. To read an object, you can use readObject(), which is a member of ObjectInputStream. However, these may not be applicable to what you are actually trying to do.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Allan -
Just a few quick general pointers:
1. You wouldn't sort a class. You would build a collection of the class's instances, then sort the collection. It would be up to you to define how the comparison for sorting is done, by overriding the equals() method.
2. Look into "serializable".
3. Look into the File class and the output writers/streams. (Writers are for text-based data, streams for binary data.)
Sun has online tutorials that help a lot with learning the Java technology. (I posted these general statements because I don't know what level you're at in Java, whether beginner or more advanced.)
Hope that helps!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. You wouldn't sort a class. You would build a collection of the class's instances, then sort the collection. It would be up to you to define how the comparison for sorting is done, by overriding the equals() method.

The equals() method isn't going to help with sorting. Instead, look at the Comparable and Comparator interfaces, and at the static method java.util.Arrays.sort() .
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand (well, sit) corrected on equals(). I think my brain was back at old C comparison library functions, which return a negative value, 0, or a positive value, depending on the comparison done.
My apologies for the confusion!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic