How to use readline() method in Java?
I advise you to go with Scanner
instead of DataInputStream
. Scanner
is specifically designed for this purpose and introduced in Java 5. See the following links to know how to use Scanner
.
- Java Documentation
- Java Tutorial
Example
Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
Is there any other method I can use to read lines in my code to perform the function of a readLine()?
My suggestion is to identify the lines before the loop, and change its mechanism:
public static int count(String text) {
int count = 0;
boolean commentBegan = false;
String[] lines = text.split(System.getProperty("line.separator"));
for (String line:lines) {
//your logic here
}
}
Splitting the text
by the line.separator
would return all lines inside it, stored in an array
. Iterate through it and use your own logic there.
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));
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);
DataInputStream deprecated readLine() method
InputStream
is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use a Reader
of some description. To convert an InputStream
into a Reader
, use InputStreamReader
. Then create a BufferedReader
around the Reader
, and you can read a line using BufferedReader.readLine()
.
More alternatives:
- Use a
Scanner
built roundSystem.in
, and callScanner.nextLine
- Use a
Console
(obtained fromSystem.console()
) and callConsole.readLine
readLine() function not working in Java
You cannot read from a BufferedWriter, you have to use a BufferedReader.
How does BufferedReader.readLine() handle EOF or slow input?
Does that mean, that when there is something like "hello" on the input and nothing more, the readLine() will wait until the \n character comes, or is it capable of recognizin EOF or something?
It will wait until either a line terminator or end of stream is received. If end of stream was received, it will return the partial line, and null
next time.
What will the function return in situation where on the input appears "Hello" and after 5 seconds " world!"?
- If you're reading from a socket and you've set a read timeout shorter than five seconds it will throw
SocketTimeoutException
. - Otherwise if EOL or EOS appears along with " world!" or within the read timeout if set, it will return "Hello world!".
- Otherwise it will block.
Related Topics
When to Use an Assertion and When to Use an Exception
Java:Does Wait() Release Lock from Synchronized Block
Tomcat - Maxthreads VS Maxconnections
Java Threads and Number of Cores
Spring Cache with Collection of Items/Entities
How to Change the Default Application Icon in Java
Qr Code Encoding and Decoding Using Zxing
Java Conventions: Use Getters/Setters Within the Class
How to Get Java to Ignore the "Trust Store" and Just Accept Whatever Ssl Certificate It Gets
On-The-Fly, In-Memory Java Code Compilation for Java 5 and Java 6
Specific Difference Between Bufferedreader and Filereader
How to Deserialize a List Using Gson or Another JSON Library in Java
Why Is This Java Code in Curly Braces ({}) Outside of a Method
JPA - Returning an Auto Generated Id After Persist()
How to Run Ssh Commands on Remote System Using Java
How to Get Annotations of a Member Variable