Powered by Blogger.

Read a file using BufferedReader and Scanner in Java

>> Thursday, July 2, 2015

Being a java developer, will get a chance to work with files using java.util and java.io packages. So mostly we use BufferedReader and Scanner Classes to read different files. These classes will contain respective methods to read files. Here, we will go through the classes and their's differences with a simple java program.
Read a file using BufferedjReader and Scanner_JavabynataraJ
java.io.BufferedReader
BufferedReader is good to read text from file, or character input streams like characters, arrays, and lines. This can process large files with default buffer size 8KB.BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads. While performing file operations read() and readLine() and close() methods are frequently used.

java.util.Scanner 
Scanner is a class to read a file line by line. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. Scanner is not synchronized so not thread safe. Using Scanner we can use mostly, delimiter(),next(),hasNext(),findInLine(),toString() and remove() methods are frequently used.

Which is better?
The choice of using a BufferedReader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice.

Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.

Create a simple HelloWorld.txt file in your C:\ drive location with the text given Here.
Here is a simple program to read a file using BufferedReader and Scanner
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
 * @author javabynataraj.blogspot.com 
 */
public class ReadFile {
 
 public static void main(String[] args) throws IOException {
  //Read a file using Scanner class
  Scanner scan = new Scanner(new FileInputStream("C:/HelloWorld.txt"));
  while(scan.hasNext())
   System.out.println(scan.nextLine());
  scan.close();
  System.out.println("nn");
  //Read a file using BufferedReader class
  BufferedReader bread = new BufferedReader(new InputStreamReader(new 
       FileInputStream("C:/HelloWorld.txt")));
  String str = bread.readLine();
  while(str!=null){
   System.out.println(str);
   str = bread.readLine();
  }
 }
}

Output:
StackOverflow discussion on BufferedReader vs Scanner

Similar Links: 
Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.