Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question on writing texts and strings to a binary file.

Started by tom_mai78101 Jun 7, 2013 at 6:54 PM 1 replies 1.5k views
Original Post
tom_mai78101
tom_mai78101

Using DataInput/OutputStreams to handle byte arrays:

  1. What do you usually do to handle text in a byte array just before writing the data to a file?
  2. Is it best practice to write texts and strings to a binary file over than to a text file when it comes to saving? (Not considering readability or compactness, but rather just usefulness)

Thanks.

Servant of the Lord
Servant of the Lord

I'm no pro, so I'd be interested to hear what others do as well.

1) For binary files that have text in them, I just write the size of the string and then the string itself - I don't do anything special to it.

2) I keep text-only data as plaintext files, for convenience of editing with tools like notepad, unless I have a reason why binary would be better.

Even things like std::maps or std::vectors I sometimes just load from comma-separated or newline-seperated text files for easier editing. Classes or structs I usually save to binary files or else plain-text config files with special syntax.

[Edit:] Oh, you were referring to Java specifically - my bad. My response comes from using C++.

Glass_Knife
Glass_Knife

Using DataInput/OutputStreams to handle byte arrays:

  1. What do you usually do to handle text in a byte array just before writing the data to a file?
  2. Is it best practice to write texts and strings to a binary file over than to a text file when it comes to saving? (Not considering readability or compactness, but rather just usefulness)

Thanks.

I would expect to see classes implementing the java.io.InputStream and java.io.OutputStream interfaces used for bytes of data (not text), and classes implementing the java.io.Reader and java.io.Writer for messing with character streams. There is nothing wrong with using an InputStream for text, but the reader and writer family have extra methods making text processing easier. For instance, the BufferedReader.readLine() method that reads an entire line and returns it as a String.

If you do text stuff with straight input streams and output streams you can wind up re-inventing the wheel.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.