Read a Text File using Scanner
Table of Contents
- Introduction
- Understanding File Reading in Java
- Key Components of Java File Reading Using Scanner
- Code Explanation
- Conclusion
1. Introduction
File reading is a common task in many Java applications. One simple and effective way to read text files in Java is by using the Scanner class. In this article, we will explore how to use the Scanner class to read a text file line by line, which is particularly helpful when working with structured text data.
The example provided is designed to be beginner-friendly, with step-by-step explanations of the code and its purpose.
2. Understanding File Reading in Java
The Scanner class, part of the java.util package, is typically used for reading input. However, it is also a powerful tool for reading files. Combined with the try-with-resources feature in Java, the Scanner class allows us to efficiently read files while managing resources.
Why use the Scanner class for file reading?
- Easy to implement for simple text file reading.
- Built-in methods for parsing primitive types, tokens, and strings.
- Ideal for beginners as it simplifies file handling without much overhead.
3. Key Components of Java File Reading Using Scanner
3.1. The Scanner Class
The Scanner class is used to read the file’s content. In this case, we are reading from a text file named test.txt, which is located in the studyeasy directory. Here’s how you create a Scanner object to read a file:
1 |
Scanner sc = new Scanner(new File("studyeasy//test.txt")); |
3.2. Using try-with-resources
We use the try-with-resources block to ensure that the Scanner resource is closed automatically after use. This is crucial for file handling as it avoids resource leaks:
1 2 3 |
try (Scanner sc = new Scanner(new File("studyeasy//test.txt"))) { // Reading file content } |
3.3. Reading File Line-by-Line
The hasNext() method in the Scanner class is used to check whether there are more lines to read in the file. The nextLine() method retrieves each line from the file:
1 2 3 4 |
while(sc.hasNext()) { String line = sc.nextLine(); System.out.println(line); } |
4. Code Explanation
4.1. Code Walkthrough
Here’s the full Java code for reading a text file using the Scanner class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.studyeasy; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(new File("studyeasy//test.txt"))) { String line; while (sc.hasNext()) { line = sc.nextLine(); System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
4.2. Output
When the code is executed, each line of the file is printed to the console. Assuming the content of the test.txt file is as follows:
1 2 |
Hello, World! This is a test file. |
The output will be:
1 2 |
Hello, World! This is a test file. |
5. Conclusion
Reading files in Java using the Scanner class is a simple and efficient method for small text files. The use of try-with-resources ensures that the resources are managed automatically, and the Scanner class provides intuitive methods like hasNext() and nextLine() to process the file content line-by-line.
Differences Between Scanner and BufferedReader
- Scanner is more versatile for parsing primitive data types and strings, making it suitable for simple text processing.
- BufferedReader is better for reading large files because it buffers input, providing better performance for large-scale file handling.