How to Use Scanner to Read File in Java - Simple Guide
To read a file in Java using
Scanner, create a Scanner object with a File as input. Then use methods like nextLine() to read the file line by line until the file ends.Syntax
Here is the basic syntax to read a file using Scanner:
File file = new File("filename.txt");- creates a file object for the file.Scanner scanner = new Scanner(file);- opens the file for reading.while(scanner.hasNextLine())- loops while there are lines to read.String line = scanner.nextLine();- reads one line from the file.scanner.close();- closes the scanner to free resources.
java
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFileExample { public static void main(String[] args) throws FileNotFoundException { File file = new File("filename.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } }
Example
This example reads a file named example.txt line by line and prints each line to the console.
java
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerFileRead { public static void main(String[] args) { try { File file = new File("example.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } } }
Output
Hello, this is line 1.
This is line 2.
End of file.
Common Pitfalls
1. Forgetting to handle FileNotFoundException: The Scanner constructor throws this exception if the file does not exist. Always use try-catch or declare the exception.
2. Not closing the Scanner: This can cause resource leaks. Always call scanner.close() after reading.
3. Using next() instead of nextLine(): next() reads only one word, not the whole line.
java
/* Wrong way: Not closing Scanner and using next() */ File file = new File("file.txt"); Scanner scanner = new Scanner(file); while(scanner.hasNext()) { System.out.println(scanner.next()); // reads word by word, not line } // scanner.close() missing /* Right way: */ File file = new File("file.txt"); Scanner scanner = new Scanner(file); while(scanner.hasNextLine()) { System.out.println(scanner.nextLine()); // reads full line } scanner.close();
Quick Reference
- File file = new File("path"); - create file object
- Scanner scanner = new Scanner(file); - open file for reading
- scanner.hasNextLine() - check if more lines exist
- scanner.nextLine() - read next line
- scanner.close() - close scanner to release resources
Key Takeaways
Use Scanner with a File object to read files line by line in Java.
Always handle FileNotFoundException when opening files with Scanner.
Close the Scanner after reading to avoid resource leaks.
Use hasNextLine() and nextLine() to read full lines, not next() which reads words.
Wrap file reading code in try-catch or declare exceptions to handle errors gracefully.