Originally posted by Landon Blake:
I'm pretty new to Java, and I had a question about input/output.
There are about a dozen different ways to do this sort of thing. Your particular situation will make some solutions more useful than others (i.e. if a human has to read the stored file, a CSV or XML is better than Object Serialization).
Object Serialization turnes the state of a Java Object into a stream which can be written to file or sent across a network.
Random Access Files can store Java primitives, but it's up to you to determine the file format and write the code to distill the Java object's state and to recreate it.
Java IO Streams can be used to store the state of an object as a readable text file, a CSV or a binary file. Again, it is left to the programmer to write the code to read and write the object state.
XML is a popular solution to the problem of having a bunch of data in a text file and not knowing what it means. While Java provides XML parsers, it is still up to the programmer to create usable objects out of it.
The way I see it, if you are just storing a few object's states, serialization is the way to go. If you need to be able to read the stored states, or want to be able to use a file input to feed data to your program, some IO Stream (BufferedReader, for instance) may be the ticket. If other people will be specifying inputs, XML can be a good solution because you can specify a DTD (document type declaration) to enforce the format of the document. Random Access Files are a good choice if you are storing a bunch of data and need to read it from various places as a user interacts with a program. Standard engineering tradeoffs.