This write-up provides a profound understanding of how to read data from a file in java and it is organized as follows:
- Different Ways of Reading Data from a File
- What is Scanner Class in Java
- How to work with Scanner Class
- How to Read Data Using Scanner Class
So, let’s start!
Different Ways of Reading Data from a File
Java provides multiple predefined classes that can be utilized to read the data from a file and some of them are listed below:
Scanner Class: Provides different methods to Read Data from a file.
FileReader Class: Reads data from a file by means of characters.
BufferedReader Class: Reads data from Character-input streams.
FileInputStream Class: Reads data in the form of bytes.
In this write-up, we will provide a detailed understanding of how to read data using the Scanner class, however, you can use any of the above-mentioned classes depending upon your needs.
Scanner Class in Java
It is a predefined class that belongs to java.util package and can be used for reading the data from a file. The Scanner class provides multiple methods to read the data from a file. In this write-up, we will utilize some of its methods to read the data from a specific file.
How to work with Scanner Class
In java, firstly, we have to import the specific class from the respective package to avail the functionalities of any inbuilt class. To import a single class or the entire package the import keyword is used, and afterward, we can create the object of the class and utilize it anywhere in the program to avail the services of such predefined java class.
import java.util.Scanner;
import java.io.FileNotFoundException;
In the above code snippet, we import the three predefined classes: a File class, FileNotFoundException class, and Scanner class.
How to Read Data Using Scanner Class
We can utilize some built-in methods of the Scanner class to read the content of any specific file.
Example
In the below code snippet, we create the object of the Scanner class and specify the name and path of the file from where we want to read the data. Moreover, we utilize the try-catch statements to handle the exceptions.
public static void main(String[] args) {
try {
File fileObj = new File("C:\\Users\\DELL\\Desktop\\file1.txt");
Scanner scanObj = new Scanner(fileObj);
while (scanObj.hasNextLine()) {
String data = scanObj.nextLine();
System.out.println(data);
}
scanObj.close();
} catch (FileNotFoundException excep) {
System.out.println("Error");
excep.printStackTrace();
}
}
}
In this example, we utilize the hasNextLine() method within the loop that will check whether a line left if yes then it will return true and our loop will continue iterating until it gets a false value. Next, we utilize the nextLine() method to get a String, and finally, we print the String:
The above snippet verifies that the Scanner class succeeds in reading the data of “file1.txt”.
Conclusion
To read data from a file in java the Scanner class and its method nextLine() is used. First, import the Scanner and File classes, specify the file name and path while object creation of File class. Next, create the object of the Scanner class and pass the object of the File class to the Scanner class. Afterward, the built-in methods of Scanner class such as hasNextLine(), and nextLine() can be used combinedly to read the data of the specified file. This write-up presents a comprehensive overview of how to read data from a file using the scanner class.