Java – Read line using InputStream

We Are Going To Discuss About Java – Read line using InputStream . So lets Start this Java Article.

Java – Read line using InputStream

Advertisements
  1. Java – Read line using InputStream

    You should use BufferedReader with FileInputStreamReader if your read from a file
    BufferedReader reader = new BufferedReader(new FileInputStreamReader(pathToFile));
    or with InputStreamReader if you read from any other InputStream
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

  2. Java – Read line using InputStream

    You should use BufferedReader with FileInputStreamReader if your read from a file
    BufferedReader reader = new BufferedReader(new FileInputStreamReader(pathToFile));
    or with InputStreamReader if you read from any other InputStream
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

Solution 1

Advertisements

You should use BufferedReader with FileInputStreamReader if your read from a file

BufferedReader reader = new BufferedReader(new FileInputStreamReader(pathToFile));

or with InputStreamReader if you read from any other InputStream

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

Then use its readLine() method in a loop

while(reader.ready()) {
     String line = reader.readLine();
}

But if you really love InputStream then you can use a loop like this

InputStream stream; 
char c; 
String s = ""; 
do {
   c = stream.read(); 
   if (c == '\n')
      break; 
   s += c + "";
} while (c != -1);

Original Author Tommaso Pasini Of This Content

Solution 2

Advertisements

TL;DR

Advertisements

Use BufferedReader within the try-with block, which will close the resource after finishing with it.

Advertisements

It is possible to read the input stream with BufferedReader and with Scanner. If you don’t have a good reason, it is better to use BufferedRead (for broad discussion BufferedReader vs Scanner see).

I would also suggest using the Buffered Reader with try-with-resources to make sure the resource are auto-closed. see

See the following code

try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
        while (reader.ready()) {
            String line = reader.readLine();
            System.out.println(line);
        }
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Original Author Memin Of This Content

Solution 3

For files, the following will let you read each line:

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;

public static void readText throws FileNotFoundException(){

     Scanner scan = new Scanner(new File("filename.txt"));

     while(scan.hasNextLine()){
         String line = scan.nextLine();

     }
}

Original Author Rana Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment