How do I read a line of input at a time in Java?

How do I read a line of input at a time in Java?

The standard input stream is represented by the InputStream object System.in. For reading a whole line at a time BufferedReader is chained to an InputStreamReader that in turn is chained to System.in.
For example: This program read line from a file “students” and displays it on screen.

import java.io.*;
import java.util.*;
public class stud
{
     public static void main (String args[ ]) throws IOException
     {
        BufferedReader br= new BufferedReader(new InputStreamReader(newFileInputStream(“students”)));
        String s= “”;
        while ((s= br.readLine())!=null) System.out.println(s);
     }
catch (IOException e)
  {
     System.out.println(e);
  }
}
Importance of Thread synchronization for multithreaded programs
Threads share the same memory space, i.e., they can share resources. However, there are critical situations where it is desirable that only one thread...
How to create a thread and start it running.
A thread in Java is represented by an object of the Thread class. Implementing threads is achieved in one of two ways:...
When should we use an event adapter class?
A listener’s class has to implement Listener interface which means all the methods of the interface must be defined in the class...
Post your comment