• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Two characters read from file come in the same with BufferedReader

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic