How to read contents of a file using Scanner class?


From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).

To read various datatypes from the source using the nextXXX() methods provided by this class.

Reading the contents of a file −

To read the contents of a file, Scanner class provides various constructors.

Sr.No
Constructors and Description
1
Scanner(File source)
Used to read data from the file represented by the given File object.
2
Scanner(InputStream source)
Used to read data from the specified InputStream.
3
Scanner(Path source)
Used to read data from the file represented by the specified file object.

Assume we have a file myFile.txt in the path D:\ as shown below −

Following examples demonstrate how to read data from this file using the above constructors.

Example- File Object

  • Create a File object representing your required file.
  • Create a Scanner class by passing the above created file object.
  • The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the next line in the file. Using these methods read the contents of the file.
import java.io.File;
import java.util.Scanner;
public class ContentsOfFile {
   public static void main(String args[]) throws Exception {
      //Creating the File object
      File file = new File("D:\\MyFile.txt");
      //Creating a Scanner object
      Scanner sc = new Scanner(file);
      //StringBuffer to store the contents
      StringBuffer sb = new StringBuffer();
      //Appending each line to the buffer
      while(sc.hasNext()) {
         sb.append(" "+sc.nextLine());
      }
      System.out.println(sb);
   }
}

Output

Hi welcome to Tutorialspoint ....

Example- InputStream Object

  • Create an FileInputStream object by passing the path of the required file to its constructor, as a parameter.
  • Create a Scanner class by passing the above created FileInputStream object.
  • The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the next line in the file. Using these methods read the contents of the file.
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;
public class ContentsOfFile {
   public static void main(String args[]) throws Exception {
      //Creating the File object
      InputStream inputStream = new FileInputStream("D:\\MyFile.txt");
      //Creating a Scanner object
      Scanner sc = new Scanner(inputStream);
      //StringBuffer to store the contents
      StringBuffer sb = new StringBuffer();
      //Appending each line to the buffer
      while(sc.hasNext()) {
         sb.append(" "+sc.nextLine());
      }
      System.out.println(sb);
   }
}

Output

Hi welcome to Tutorialspoint ....
raja
Published on 01-Aug-2019 17:38:57
Advertisements