Question on writing texts and strings to a binary file.

Started by
1 comment, last by Glass_Knife 10 years, 10 months ago

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.

Advertisement

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++.

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.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement