aspose file tools*
The moose likes I/O and Streams and the fly likes Two characters read from file come in the same with BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Two characters read from file come in the same with BufferedReader" Watch "Two characters read from file come in the same with BufferedReader" New topic
Author

Two characters read from file come in the same with BufferedReader

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I'm reading in data from a file which is "packed" (each two values packed into one bit). I can view this data with an unpack utility to verify that it is correct. However, when I bring it in to my Java app, it reads two characters in wrong.. ie: reads both 24 and 85 in as 45.

My method of reading in from the file is using BufferedReader.readLine() into a String variable and then using String.getBytes() to then begin parsing the string to "unpack" it.

As stated, it works in all cases except two values.

In an attempt to get a unique value for each so I can individually parse them, I have tried to out to the command line things like the ASCII value, Integer.decode, and String.hashCode... but they all seem to be read in as identicle values even though I have proven they are not identicle in my other viewer.

Any suggestions as to tests to try against the two in order to prove them not equal are appreciated and will be attempted.

If using BufferedReader into a string then getBytes is my problem, I will attempt to change it to your suggestion.

Thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jeff,

All "Readers" expect to work with valid character data, and will do some translation in a platform- and character-set-dependent way. It sounds like you don't have character data at all, but encoded data, which you have to treat as raw binary. InputStreams, as opposed to Readers, will never modify the data stream for you. That's what you want.

So use a FileInputStream to read the data; don't try to read a line at a time, just read into a byte[]. Create a decoded byte[] yourself, and then you can use the appropriate String constructor to turn the decoded data into a String.


[Jess in Action][AskingGoodQuestions]
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Thank you very much Ernest. Changing my input from BufferedReader to FileInputStream has given me the proper results! Now I'm trying to move my application to use the FIS instead of the BR which is proving to be a little more quirky than just changing class names, but it's coming along nicely.

Thank you for your help!
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Originally I was still doing my Buffered Read into a string to get the length of a single line and create a byte array of the appropriate length of that line.

But I expect there must be an easier/more efficient way.. suggestions?
 
Don't get me started about those stupid light bulbs.
 
subject: Two characters read from file come in the same with BufferedReader