HudaTutorials.com

Java BufferedReader Class

Last updated on

java.io.BufferedReader Class

BufferedReader Class in Java

The Java BufferedReader class reads text from a character input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. To perform console input use a byte stream. Today, using a byte stream to read console input is still acceptable.

For commercial applications, the preferred method of reading console input is to use a character oriented stream. This makes your program easier to internationalize and maintain. In Java, console input is accomplished by reading from System.in. To obtain a character based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream.

System.in refers to an object of type InputStream, it can be used for inputStream.

In this tutorial you can learn about java io BufferedReader class. And how to use java io BufferedReader class examples.

What is Java BufferedReader Class ?

The Java BufferedReader class is used to read the stream of characters from the specified source.

How to read the characters from the console

The following program shows how to get input from console using java.

/*	How to read the characters from the console Example
	Save with file name BufferedReaderExample.java	*/
import java.io.*;
public class BufferedReaderExample
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// WE SHOULD USE try-catch BECAUSE MOST OF THE
		// JAVA I/O CLASES THROWS IOException
		try
		{
			// DECLARE THE PREMITIVE DATA TYPE char
			char c;
			ir = new InputStreamReader(System.in);
			br = new BufferedReader(ir);
			System.out.println("Enter Characters, q to Quit : ");
			do
			{
				c = (char) br.read();
				System.out.println("Char : "+c);
			}while(c != 'q');
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

How to read the lines of text from the console

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

/*	How to read the lines of text from the console Example
	Save with file name BufferedReaderExample2.java	*/
import java.io.*;
public class BufferedReaderExample2
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		try
		{
			String str;
			ir = new InputStreamReader(System.in);
			br = new BufferedReader(ir);
			System.out.println("Enter some lines of text, stop to Quit : ");
			do
			{
				str = br.readLine();
				System.out.println("Line Text is : "+str);
			}while(!str.equals("stop"));
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read the text from file using Java BufferedReader

How to print only directory names using java? How to get only directories from given path using java?

/*	How to read the text from file using Java BufferedReader Example
	Save with file name BufferedReaderExample3.java	*/
import java.io.*;
public class BufferedReaderExample3
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		try
		{
			int ch=0;
			fis = new FileInputStream("BufferedReaderExample3.java");
			ir = new InputStreamReader(fis);
			br = new BufferedReader(ir);
			while((ch = br.read()) != -1)
			{
				// CONVERT byte TO char
				System.out.print( (char) ch);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read total bytes from file into array using Java BufferedReader

How to print available file system root names using java? How to get file system roots using java? How to get available drives using java?

/*	How to read total bytes from file into array
	using java BufferedReader Example
	Save with file name BufferedReaderExample4.java	*/
import java.io.*;
public class BufferedReaderExample4
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		// ARRAY TO READ BYTES
		char charbuff[] = null;
		try
		{
			fis = new FileInputStream("BufferedReaderExample4.java");
			ir = new InputStreamReader(fis);
			br = new BufferedReader(ir);
			// GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
			charbuff = new char[fis.available()];
			// READ TOTAL BYTES FROM FILE
			br.read(charbuff, 0,fis.available());
			// PRINT CHARACTERS FROM ARRAY
			for(int j=0;j<charbuff.length;j++)
			{
				// CONVERT byte TO char
				System.out.print( (char) charbuff[j]);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				// RELEASE ARRAY MEMORY
				charbuff = null;
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read text line by line from file using Java BufferedReader

/*	How to read text line by line
	from file using java BufferedReader Example
	Save with file name BufferedReaderExample5.java	*/
import java.io.*;
public class BufferedReaderExample5
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		// String TO READ LINE FROM FILE
		String line = null;
		try
		{
			fis = new FileInputStream("BufferedReaderExample5.java");
			ir = new InputStreamReader(fis);
			br = new BufferedReader(ir);
			// GET THE LINE FROM FILE
			while((line = br.readLine()) != null)
			{
				// PRINT THE LINE
				System.out.println(line);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				// RELEASE MEMORY
				line = null;
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read bytes from file using Java BufferedReader

/*	How to read bytes from file using
	java BufferedReader Example
	Save with file name BufferedReaderExample6.java	*/
import java.io.*;
public class BufferedReaderExample6
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		// int TO READ byte FROM FILE
		int ch;
		try
		{
			fis = new FileInputStream("BufferedReaderExample6.java");
			ir = new InputStreamReader(fis);
			br = new BufferedReader(ir);
			int i = 1;
			// GET THE bytes FROM FILE
			while((ch = br.read()) !=-1)
			{
				// PRINT THE CHARACTER
				System.out.print((char) ch);
				if( (i%10) == 0)
					br.skip(3);
				i++;
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read bytes from file using java BufferedReader

/*	How to read bytes from file using
	java BufferedReader Example
	Save with file name BufferedReaderExample7.java	*/
import java.io.*;
public class BufferedReaderExample7
{
	public static void main(String args[])
	{
		// java.io.BufferedReader DECLARATION
		java.io.BufferedReader br = null;
		// InputStreamReader DECLARATION
		InputStreamReader ir = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		// int TO READ byte FROM FILE
		int ch;
		try
		{
			fis = new FileInputStream("BufferedReaderExample7.java");
			ir = new InputStreamReader(fis);
			br = new BufferedReader(ir);
			int i = 1;
			if(br.ready())
			{
					// GET THE bytes FROM FILE
					while((ch = br.read()) !=-1)
					{
						// PRINT THE CHARACTER
						System.out.print((char) ch);
						if(br.markSupported())
						{
							if(i<30)
							{
						if((i%10) == 0)
							br.mark(i);
						if((i%20)==0)
						{
							br.reset();
						}
							}
							if((i%30)==0)
								break;
						}
						i++;
					}
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(br!=null)
					br.close();
				if(ir!=null)
					ir.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}