How to Use Readline() Method in Java

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 round System.in, and call Scanner.nextLine
  • Use a Console (obtained from System.console()) and call Console.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!"?

  1. If you're reading from a socket and you've set a read timeout shorter than five seconds it will throw SocketTimeoutException.
  2. Otherwise if EOL or EOS appears along with " world!" or within the read timeout if set, it will return "Hello world!".
  3. Otherwise it will block.


Related Topics



Leave a reply



Submit