4

I was reading about Java I/O and found some interesting areas like streams, readers etc.

 InputStream input = new FileInputStream("input-file.txt");
 int data = input.read();
 while(data != -1){
   data = input.read();
 }

I can do the same thing by using Readers as follows:

Reader reader = new FileReader("input-file.txt");
 int data = reader.read();
 while(data != -1){
     char dataChar = (char) data;
     data = reader.read();
 }

As I know, Streams are used to retrieve input from continuously flowing data.

Now I am confused with the difference between Streams & readers; and if we wraps the stream with a buffered reader - how it break lines, since stream is a continuously flowing thing.

I found some reference sites like this site. But I can't understand the difference.

Please can someone please help me to understand?

5

1 Answer 1

4

Readers are to read text data with particular character encoding (UTF-8, ISO etc..)

while on the other hand, streams are binary data.

They work same but there parent classes are different.

in a nutshell, if you have to read binary data and save it somewhere, use stream.

If you have to read text in a particular encoding and then play with it, then use readers.

Hope this answers.

Sign up to request clarification or add additional context in comments.

4 Comments

So basically readers what readers doing is read from stream and encode to characters. am i right. and if we wraps the stream with a buffered reader, how it break lines since stream is a continuously flowing thing ?
line breaks are read by newline character \n . this functionality is inside readLine(). It reads characters until it finds \n.. Yes you are right in first part.
so, if i'm using bufferedReader , but doesn't have line breaks.
whole text means, whole buffered string until that time ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.