I use InputStream to read some data, so I want to read characters until new line or '\n'.
Asked
Active
Viewed 1.4e+01k times
39
-
3Use a `BufferedReader` and its `readLine` method. Dupe of: http://www.stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java – Tunaki Jan 22 '16 at 19:41
3 Answers
103
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);

Datz
- 3,156
- 3
- 22
- 50

Tommaso Pasini
- 1,521
- 2
- 12
- 16
-
I was hoping for something bulid-in :/. Btw. BufferedReader doesn't support encoding. – Radim Nyč Nov 22 '17 at 15:00
-
7What do you mean by built in? BufferedReader is in the java standard library and it actually does, you can specify the encoding in this way: ``new BufferedReader(new InputStreamReader(new FileInputStream("path/to/file"), "UTF8"));`` – Tommaso Pasini Nov 23 '17 at 15:08
-
Didn't know that. I found that you can do this with InputStream and then I wanted to find out if there is any way how to make InputStream readLines without a while cycle. I will try mentioned BufferedReader constructor... – Radim Nyč Dec 03 '17 at 18:59
-
1well for anyone who stumbles on this in the future like me, it's FileReader not FileInputStreamReader – Krusty the Clown Dec 23 '21 at 19:52
-
reader.ready() does not always return true, leading to no lines read. More failure proven: String line = null; while ((line = reader.readLine()) != null) { ... } – kaiser Oct 18 '22 at 14:29
10
TL;DR
Use BufferedReader
within the try-with
block, which will close the resource after finishing with it.
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();
}

Memin
- 3,788
- 30
- 31
5
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();
}
}

Rana
- 1,675
- 3
- 25
- 51
-
6
-
3Yes it does if slightly rewritten - `var scan = new Scanner(InputStream source)` – Kong Aug 15 '19 at 14:10
-
The code with `val scan = Scanner(inputstream)` solved my problem reading chunked HTTP responses. – rwst Jun 11 '22 at 16:05