File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Reading integers from raw text files Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "Reading integers from raw text files" Watch "Reading integers from raw text files" New topic
Author

Reading integers from raw text files

Jp Coyne
Greenhorn

Joined: Feb 24, 2011
Posts: 1
Hi
This is my 1st post so please be gentle
I'm currently trying to read in a raw text file of ints in an android application and running into all sorts of issues. Currently I have the code -



This works great, except that it reads the the ascii code of the int instead of the value, eg. 2 in my file turns into 50. I've tried different ways of reading the file such as using dataInputStreams and bufferedInputSteeams but these don't seem to compile at all so I think I'm barking up the wrong tree with that approach. As far as I can see I have 2 options, store the ints in a different format like a database (seems like overkill), convert the ascii codes to there represented values in some way (should work if I knew how).
Does anyone have any ideas? I've chased my tail all over the net with this one.

Thanks
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 4875

The InputStream.read() method reads a single byte. If you created a text file with a "2" in the first position, that "2" is an ASCII character, and "2" in ASCII is really 0x32 in hex, and 0x32 in hex is 50 as an integer.

If you want to read ASCII (or UTF-8) characters out of a file you must use a text input method, not a binary input method. Try the Reader.read() methods or BufferedReader.readline().


JBoss In Action
 
 
subject: Reading integers from raw text files
 
Threads others viewed
IO Streams and Characters.
How to encode image in parts?
Converting byte to BitSet
Advice on InputStream reading
decodeStream() OR Put large file in ASSETS folder
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com