Sign in to follow this  
tom_mai78101

Question on writing texts and strings to a binary file.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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.

Edited by Glass_Knife

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this