I'm using an InputStream
to read some data and I want to read characters until new line or '\n'.
The InputStream
is a generic input stream of bytes not tight to any specific implementation or any source (like a file).
You should use BufferedReader
with FileReader
if your read from a file
BufferedReader reader = new BufferedReader(new FileReader(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);