Skip to toolbar

Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).

An alternate method to take user input is BufferedReader class. Unlike Scanner class, BufferedReader is fast and simple.

If you don’t have read about Scanner Class, would request you to please have a look and understanding of Scanner Class, please visit the previous chapter:

Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).



BufferedReader
BufferedReader

BufferedReader

As mentioned in the above image it is clear that the BufferedReader methods use an instance of InputStreamReader class as mentioned below:

InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);

Mainly there are two methods to read data from the user:

String userName=br.readLine();

It reads the entire line at once till enter is pressed.



int userNum=br.read();

It reads single character as input but returns its integer value(ASCII) value.

System.out.println(userNum);

System.out.println((char)userNum);

Output of above two statements would be seperate.

Code Example

/**
 * 
 */
package userInputs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author ashok.kumar
 *
 */
public class UserInputBufferedReader {
	
	public static void main(String args[]) throws IOException
	{
		InputStreamReader isr= new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		System.out.print("Enter your address: ");
		// readLine() will read entire line at once
		String userName=br.readLine();
		
		// read() will read single character and returns its integer value(ASCII) value.
		System.out.print("Enter any character(numeric/char/special char): ");
		int userNum=br.read();
		
		System.out.println("\nOutput via readLine(): "+userName);
		System.out.println("Output via read(), returns ASCII value: "+userNum);
                // Convert numeric value into char.
		System.out.println("Output via read(), after conversion: "+(char)userNum); 	
	}
}

Output of the above code:

Enter your address: A, 26 Tower/C, NY.
Enter any character(numeric/char/special char): $

Output via readLine(): A, 26 Tower/C, NY.
Output via read(), returns ASCII value: 36
Output via read(), after conversion: $


Related Links:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:


Reading data from external source

/**
 * 
 */
package userInputs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author ashok.kumar
 *
 */
public class UserInputBufferedReader_2 {
	
	public static void main(String[] args) throws IOException {
		
		// Locating external source file on the system.
		FileInputStream file=new FileInputStream("src\\userInputs\\UserInputBufferedReader_2.java");
		
		// Create instance of the source file.
		InputStream inputS=file;
		
		// Convert data from file to Stream, so that it can be used by the compiler.
		InputStreamReader isr=new InputStreamReader(inputS);
		
		// Load the data into the BufferedReader instance to read data from file.
		BufferedReader br=new BufferedReader(isr);
		
		// Read the data from file line by line till end of file.
		while(br.readLine()!=null)
		{
		System.out.println(br.readLine());
		}
	}
}

Output:

The output of above code would be same as we have set source file same java file.



Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 7
  • Page views today : 20
  • Total visitors : 49,394
  • Total page view: 68,004

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.