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.



2085total visits,4visits today

Leave a Reply

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