• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What InputSteam is to use in which seneraio?

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have a question that there are many input stream like Scanner,BufferedReader...

I want to know how to decide which stream to use when?


Thanks
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner and BufferedReader technically aren't InputStreams (at least not java.io.InputStream)
They are both classes that utilise them though, so I'll read the question in that you are discussing different ways of handling input.

Looking at the API for these classes:
Buffered Reader: Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Scanner: A simple text scanner which can parse primitive types and strings using regular expressions.

So if I you want to read in big lump of text then BufferedReader seems appropriate.
If you want to interpret and parse it in a smarter manner, maybe the Scanner is what you are after.
You could still use BufferedReader to read the input line by line, and do string operations on that rather than using the scanner. In fact that's what many people were doing before the Scanner came along in Java5 (and some are probably still doing regardless)

Is it wrong to use the BufferedReader in that scenario? - I don't think so. So long as it works.
Is it more complicated than the Scanner equivalent? Possibly. If it can be expressed in regular expressions, then the Scanner may be easier to code/more efficient. But then maybe not too if you have some massively weird processing.

In general: Read the API, understand the classes, and use them in the appropriate scenario.
That is where your judgement has to come into play. There is never just one answer, and people will debate over which may be the 'best' answer.
You have to consider things like amount of effort required, readability, flexibility of code etc etc.

Welcome to the wonderful world of programming.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stefan Evans

Thanks At least you give me a way How to fish..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One important thing to be aware of when you are doing I/O with Java:

InputStream and OutputStream are for reading bytes (binary data).

Reader and Writer are for reading text (characters, strings). Readers and writers are a layer on top of streams, to convert the bytes that are read to characters, or to convert the characters that are written to bytes - because ultimately the only thing a computer can store (in memory, or on a disk) are bytes.

Converting bytes to characters or vice versa is done with a character encoding. Some well-known character encodings are ASCII, UTF-8 and ISO-8859-1.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Reader and Writer are for reading text (characters, strings). Readers and writers are a layer on top of streams, to convert the bytes that are read to characters, or to convert the characters that are written to bytes - because ultimately the only thing a computer can store (in memory, or on a disk) are bytes.

Converting bytes to characters or vice versa is done with a character encoding. Some well-known character encodings are ASCII, UTF-8 and ISO-8859-1.



@Jesper de Jong
Great points

Thanks
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote: . . . Scanner: A simple text scanner . . .

I always think that line in the API is confusing. It gives the impression that Scanner is simple, but I think it is supposed to mean that the text is simple. Plain text, as opposed to what you used to get with word processors, lots of control characters.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been through the Java® Tutorials?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell

Thanks
<quote>
Have you been through the Java® Tutorials?
</quote>

I am going through this tutiorals..
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic