• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic