jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Storing... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing..." Watch "Storing..." New topic


Yoni Horwitz

Joined: Jul 01, 2002
Posts: 3
What's the best way to store data?
I've coded in Eiffel, and there's a STORABLE class that worked out pretty well... Create a class that inherits from STORABLE, and you can store it no problem...
In Java, though, what's the best way to do this?
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
I assume that you mean storing in a file. The Java solution is very similar to your description of the Eiffel solution. In java, the class to store must implement the Serializable interface.
class StorageClass implements Serializable
Then, you should use one of the classes in the java.io package. (Note: I'm just going to talk about the output stuff. If you want to do input, just substitute "Input" for "Output" in the class names).
To output objects, you'll need an ObjectOutputStream object. If you want to output to a file, your best bet is to pass the ObjectOutputStream constructor a FileOutputStream object. One of the constructors of the FileOutputStream can take in a String specifying the path of the file. Like this:
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("C:\\path\\of\\file.dat"));
Then, to output:
For more information, have a look at the java.io package in the Java 2 Standard Edition API.
Yoni Horwitz

Joined: Jul 01, 2002
Posts: 3
Thanks a lot. I'll try that out.
I agree. Here's the link: http://aspose.com/file-tools
subject: Storing...
jQuery in Action, 3rd edition