Hey guys, can anyone tell me how I could use randomaccessfile to read in a txt file and copy the content to a tree? Also how to then do the opposite and write newly changed nodes from the tree onto the text file? It's pretty much like creating a save file. I have a tree, then when the program closes, I have a copy of the tree saved as a txt file, then when I boot the program back up, it reads in the text file and re populates the tree.

Recommended Answers

All 2 Replies

Do you have any code of what you have so far?

java.io.RandomAccessFile is naturally used for binary files, not text files. The problem is that text files are normally formatted according to how humans read files, not with data aligned to addresses within the file. It seems to me that it is probably a mistake to try to make a file that is both random access and human readable, but it should be possible.

You just need to create a format for your file by choosing an address for each piece of data as a number of bytes from the beginning of the file, and the number of bytes allocated to that data. When you want to read or modify that piece of data, you create a byte[] that contains the number of bytes allocated to the data, then use readFully or write to read or write that array.

Beyond that, you just need a way to construct a byte[] with your text and read a String from a byte[], which is exactly the purpose of java.io.Writer and java.io.Reader. To fill a byte[] you will want to construct one of these:

final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(size);
Writer out = new OutputStreamWriter(byteOut);

Here size will be the size of the byte[] that you want. Naturally you would write your text to the Writer, but you must also take into account the fact that the text you have written my not fill all the bytes you have allocated, and the remaining bytes need to be filled with something that belongs in a text file, like whitespace or other filler. This is why you declared byteOut above, so that you can call byteOut.size() to determine how many bytes you have written so far. If you have written too many, you will need to abort; you can't just truncate because some characters are more than one byte and you wouldn't end up with a proper text file if you chopped a character in half.

If you haven't written enough, then you will want to find a filler character that you know is represented by one byte in your charset and then write that character until byteOut.size() is exactly the value that you need. Alternatively, if all characters are represented by more than one byte in your charset, then you will need to work something out. For example, if all characters are two bytes, then you need to always allocate an even number of bytes for each piece of data.

Reading from the file is easier. You just need a java.io.ByteArrayInputStream and a java.io.InputStreamReader, and then read the text as you would read any text. The only difficulty is that you will need to deal with the filler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.