writing data in to files with java

1.2k views Asked by At

I am writing a server in java that allows clients to play a game similar to 20 questions. The game itself is basically a binary tree with nodes that are questions about an object and leaves that are guesses at the object's identity. When the game guesses wrong it needs to be able to get the right answer from the player and add it to the tree. This data is then saved to a random access file.

The question is: How do you go about representing a tree within a file so that the data can be reaccessed as a tree at a later time.

If you know where I can find information on keeping data structures like trees organized as such when writing/reading to files then please link it. Thanks a lot.


Thanks for the quick answers everyone. This is a school project so it has some odd requirements like using random access files and telnet.

4

There are 4 answers

0
Richard Pianka On

I think you're looking for serialization. Try this:

http://java.sun.com/developer/technicalArticles/Programming/serialization/

1
FreeAsInBeer On

As mentioned, serialization is what you are looking for. It allows you to write an object to a file, and read it back later with minimal effort. The file will automatically be read back in as your object type. This makes things much easier than trying to store the object yourself using XML.

0
Stephen C On

This data is then saved to a random access file.

That's the hard way to solve your problem (the "random access" bit, I mean).

The problem you are really trying to solve is how to persist a "complicated" data structure. In fact, there are a number of ways that this can be done. Here are some of them ...

  • Use Java persistence. This is simple to implement; make sure that your data structure is serializable, and then its just a few lines of code to serialize and few more lines to deserialize. The downsides are:

    • Serialized objects can be fragile in the face of code changes.
    • Serialization is not incremental. You write/read the whole graph each time.
    • If you have multiple separate serialized graphs, you need some scheme to name and manage them.
  • Use XML. This is more work to implement than Java persistence, but it has the advantage of being less fragile. And if something does go wrong, there's a chance you can fix it with XSLT or a text editor. (There are XML "binding" libraries that eliminate a lot of the glue coding.)

  • Use an SQL database. This addresses all of the downsides of Java persistence, but involves more coding ... and using a different computational model to access the persistent data (query versus graph navigation).

  • Use a database and an Object Relational Mapping technology; e.g. a JPA or JDO implementation. (Hibernate is a popular choice). These bridge between the database and in-memory views of data in a more or less transparent fashion, and avoids a lot of the glue code that you need to write in the SQL database and XML cases.

0
Jonas Fagundes On

Java serialization has some pitfalls (like when you update your class). I would serialize in a text format. Json is my first choice here but xml and yaml would work as well.

This way you would have a file that doesn't rely on the binary version of your class.

There are several java libraries: http://www.json.org

Some examples:

http://code.google.com/p/json-simple/wiki/DecodingExamples

http://code.google.com/p/json-simple/wiki/EncodingExamples

And to save and read from the file you can use the Commons Io:

import org.apache.commons.io.FileUtis;
import java.io.File;
...
File dataFile = new File("yourfile.json");
String data = FileUtils.readFileToString(dataFile);

FileUtils.writeStringToFile(dataFile, content);