Master java skills

Reading File in Java

Reading a file refers to getting the information from inside the text file. Java provides three different ways to read a text file. These are following.

  • Scanner class
  • BufferedReader class
  • File Reader class

Using Scanner class :

The Scanner class of the Java is used to read input data from several sources like – input streams, users, files, etc.

import java.util.*; 	//importing this for Scanner class
import java.io.File; 	//importing this for File
import java.io.FileNotFoundException; 	//importing this for throws an exception.

public class readingFile {
    public static void main(String[] args) throws FileNotFoundException {
        //first you need to create a text file in specified directory with the name jst.txt
        File file = new File("D:\\Java_Programs\\New Directory\\jst.txt");
        
        //passing file object to scanner class
	Scanner sc = new Scanner(file);
	
	// Condition holds true till there is character in a string
	while (sc.hasNextLine()){
            System.out.println(sc.nextLine());
            //Reading file data line by line.
        }
  }
Output :

My first choice is javaTrainingSchool !

Explanation :

In above code we have created file name jst.txt which is a text file. We have written some data in jst.text file to read by Java code. In this code we have used FileNotFoundException class for throwing an exception. If our code doesn’t find specified file in given directory then it throws an exception.

Using BufferedReader class :

BufferReader provide a number of ways to read lines of text and supports fast reading by buffering data.BufferedReader is a class of Java which extend Reader class.

import java.io.*;  // Importing input output classes

public class readingfile {
    public static void main(String[] args) throws Exception {
    
        //first you need to create a text file in specified directory with the name jst.txt.
        File file = new File("D:\\Documents\\Java_Programs\\jst.java");
	
	// Creating an object of BufferedReader class
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str = "";

        // Condition holds true till there is character in a string.
        while ((str = br.readLine()) != null){
            System.out.println(str);
        }
  }
}
Output :

Do you want to become master of java ? choose javaTrainingSchool

Using FileReader class :

FileReader class is convenience class for reading character files.

FileReader(String fileName): Creates a new FileReader, given the name/path of the file to read from.

import java.io.*;

public class readingfile {
    public static void main(String[] args) throws Exception {
        //first you need to create a text file in specified directory with the name jst.txt.
        FileReader fr = new FileReader("D:\\Documents\\Java_Programs\\jst.java");

        int ch = 0;
        // this loop will return -1 if there is nothing to read.
        while ((ch = fr.read()) != -1){
            // Print all the content of a file
            System.out.print((char)ch);
        }
    }
}
Output :
I feel very comfortable with javaTrainingSchool !

Explanation :

The read() method returns a single character in the form of an integer value that contains the character’s char value. It returns -1 when all of the data has been read and that FileReader may be closed.