permaculture playing cards*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing..." Watch "Storing..." New topic
Author

Storing...

Yoni Horwitz
Greenhorn

Joined: Jul 01, 2002
Posts: 3
Hello,
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?
Thanks,
Yoni
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:
output.writeObject(objectToWrite);
For more information, have a look at the java.io package in the Java 2 Standard Edition API.
B'Shalom,
-Stu
Yoni Horwitz
Greenhorn

Joined: Jul 01, 2002
Posts: 3
Thanks a lot. I'll try that out.
 
 
subject: Storing...
 
Similar Threads
small confusion with"meaning of persistent word "
Ants Marching
Management of data object
Relational... OO?
display DOM document