• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between System.in.read() and BufferedReader.read()

 
Nathan Russell
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first off - I hope this question is suitable for the Beginners section, and not the Intermediates ! - I certainly do not consider myself anything other than a Beginner ....... So, here goes nothing .....

Is there a difference in the return value / data type returned of System.in.read() and BufferedReader.read() ?
By this, I mean they both return an int - right ? An int being 16 bits ?

Heres the deal. I have written 2 methods within a console application. One reads from stdin into a StringBuffer object until eof; the other reads from a file into a StringBuffer object until eof.

I wont bore you with the entire code, but the 2 methods in question are:



The objective of the application is to take a stream from stdin, or read a file from disk, and produce a dump of each byte in decimal, hex, and the ascii. (A lot like debug or many other file dumpers do in fact - thats not the point, its a learning excercise for me !)

If I run the application, and pipe into it a binary file - and consequently use the readStdin() method - it 'seems' to work perfectly.
If I run the app and read the same file from disk, using the readFile() method, for the most part the output is the same, but occasionally there are differences.

If I do the same with standard text files, both methods return the same results. Its only on binary files that there seems to be a difference.

Any ideas ??
Cheers

Nathan
 
Marilyn de Queiroz
Sheriff
Posts: 9079
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedReader returns an int (32 bits): The number of characters read, or -1 if the end of the stream has been reached.

InputStream returns an int (32 bits): The total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.

Note
1) an int is 32 bits, not 16 bits
2) BufferedReader reads chars, but InputStream reads bytes.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!