• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read integers using DataInputStream

 
Jaques Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jaques Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, the actual implementation of that buffer probably varies from one type of JVM to another.
 
Jaques Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic