Read line of characters from console using InputStream - Java Language Basics

Java examples for Language Basics:Console

Description

Read line of characters from console using InputStream

Demo Code

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
 
        public static void main(String[] args) {
                 BufferedReader br =
                 new BufferedReader(new InputStreamReader(System.in));
                 //from  w  w w  . j a  v  a2s  . c o  m
                 String strLine = null;
                 
                 System.out.println("Reading line of characters from console");
                 System.out.println("Enter exit to quit");
                 
                 try
                 {
                        while( (strLine = br.readLine()) != null)
                        {
                           if(strLine.equals("exit"))
                              break;
                                       
                           System.out.println("Line entered : "  + strLine);
                                                                                                                                                                                                                               
                        }
                       
                        br.close();                    
                                               
                 }
                 catch(Exception e)
                 {
                        System.out.println("Error while reading line from console : " + e);
                 }
        }
}

Result


Related Tutorials