permaculture playing cards*
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


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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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: 19697
    
  20

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
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: 39053
    
  23
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: 39053
    
  23
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
 
GeeCON Prague 2014
 
subject: how to read integers using DataInputStream