Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing...

 
Yoni Horwitz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I'll try that out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic