• Post Reply Bookmark Topic Watch Topic
  • New Topic

What InputSteam is to use in which seneraio?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • 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
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
@Stefan Evans

Thanks At least you give me a way How to fish..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • 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: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java® Tutorials?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • 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: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!