This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to read integers using DataInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to read integers using DataInputStream" Watch "how to read integers using DataInputStream" New topic
Author

how to read integers using DataInputStream

Jaques Smith
Greenhorn

Joined: Aug 01, 2011
Posts: 5
Hello! I would need some help with the following code snippet:

The problem is that, when I run this code, the printed number is not the first number in the file. It's not even in the file. What am I doing wrong?
Thanks!
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

Without knowing what is in the file, it's impossible to say what you are doing wrong.

But my guess is that your file contains some text which represents an integer, as opposed to 32 bits which represent an integer, and you were assuming that the readInt() method was meant for the first case instead of for the second case.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20049
    
  30

Paul is right. DataInputStream is for reading binary data (which may or may not contain UTF strings). You seem to want to read character data. Check out java.util.Scanner instead. Use a FileReader instead of a FileInputStream as well:

On a side note, you don't need a RandomAccessFile to create a FileInputStream. The following will work just as fine:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jaques Smith
Greenhorn

Joined: Aug 01, 2011
Posts: 5
Thank you very much!
I still have a question related to the BufferedInputStream. I'm trying to understand how it works and all that the Java documentation says is "When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time."

What happens more precisely? After I have read all the values stored in this buffer, does it automatically read new ones from the hard disk? Or when exactly does it cause a call to the underlying system? Can I control its length? And furthermore, if I want to sort the values in the buffer, do I have to put them into an array first?

Thanks again, I really appreciate your help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43381
    
  32
I don't think you can tell more precisely, unless you download the "native" code for the JVM. It is rather like the difference between calling out individual letters to make a message, and writing those letters on a paper and passing the paper to the recipient.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43381
    
  32
In fact, the actual implementation of that buffer probably varies from one type of JVM to another.
Jaques Smith
Greenhorn

Joined: Aug 01, 2011
Posts: 5
OK, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to read integers using DataInputStream
 
jQuery in Action, 3rd edition