Master W3Schools Java Read Files: Easy Code Examples

Robert Jul 13, 2026

When it comes to processing and manipulating data in Java, file handling is an essential aspect. W3Schools, a renowned learning platform, offers comprehensive tutorials and examples for Java file I/O operations. In this article, we will explore how to read files in Java using the teachings from W3Schools, equipping you with the necessary knowledge to handle files effortlessly.

10 Examples to Read a Text file in Java [UPDATED]
10 Examples to Read a Text file in Java [UPDATED]

Before delving into the specifics, let's quickly recap Java's file handling capabilities. Java provides classes to read, write, and manipulate files using a well-defined structure. The key classes for file I/O operations include File, FileReader, and Scanner, among others. Armed with these classes, let's proceed to understand how to read files in Java.

Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained

Reading Files Using FileReader

The FileReader class in Java is used to read streams of characters from a file. It's an essential class for reading text files. Here's how to use it:

java at a glance 😎
java at a glance 😎

First, import the necessary classes and create a FileReader object associated with the file you wish to read. Then, use a BufferedReader to read the contents of the file line by line. Close the reader once you're done to free up system resources. Here's a simple Java code snippet demonstrating this:

Reading a File Line by Line

Know how to read CSV file in java
Know how to read CSV file in java

Here's a basic example of reading a file using a FileReader and BufferedReader in Java:

```java import java.io.BufferedReader; import java.io.FileReader; public class ReadFile { public static void main(String[] args) { try { FileReader fr = new FileReader("filename.txt"); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } } ```

This code reads a file named "filename.txt" line by line and prints each line to the console.

Handling File Not Found Exceptions

Java code for maintaining a student records database using File Handling
Java code for maintaining a student records database using File Handling

When reading a file, it's crucial to handle potential FileNotFoundExceptions. If the file doesn't exist, Java won't silently ignore it but rather throw a FileNotFoundException. Here's how you can handle this exception:

```java import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; public class ReadFile { public static void main(String[] args) { try { FileReader fr = new FileReader("filename.txt"); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } catch (Exception e) { e.printStackTrace(); } } } ```

Now, if the file doesn't exist, the program will print a user-friendly message instead of crashing unexpectedly.

Reading Files Using Scanner

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

The Scanner class in Java provides an easy-to-use alternative for reading text files. It's simple, intuitive, and deals with exceptions gracefully. Here's how to use Scanner to read a file:

Reading a File with Scanner

Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
a list of all vcsoe shortcuts with the names and abbreviations
a list of all vcsoe shortcuts with the names and abbreviations
FileWriter in Java (with Example) - Scientech Easy
FileWriter in Java (with Example) - Scientech Easy
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
What Is the Java Programming Language? Pros, Cons, Courses and Career Tips
What Is the Java Programming Language? Pros, Cons, Courses and Career Tips
| Pythonista Planet
| Pythonista Planet
Java Cheat Sheet Every Developer Should Save!
Java Cheat Sheet Every Developer Should Save!
two different types of words that appear to be in the same language, one is for each
two different types of words that appear to be in the same language, one is for each
Java Basics for Beginners 🚀
Java Basics for Beginners 🚀
Java Projects 2023
Java Projects 2023
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Free Java Programming Book
Free Java Programming Book
the table shows different types of code names for each language, and how to use them
the table shows different types of code names for each language, and how to use them
Operators Summary in Java ☕
Operators Summary in Java ☕
a white poster with blue and orange text on it
a white poster with blue and orange text on it
Java 8 - How to count words in a text file in Java using NIO and Streams API
Java 8 - How to count words in a text file in Java using NIO and Streams API
Java Programming Cheat Sheet | Data Types, Variables & Java Fundamentals
Java Programming Cheat Sheet | Data Types, Variables & Java Fundamentals

Here's how to read a file character by character using a Scanner in Java:

```java import java.io.File; import java.io.IOException; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { try { Scanner sc = new Scanner(new File("filename.txt")); while (sc.hasNext()) { System.out.println(sc.nextLine()); } sc.close(); } catch (IOException e) { System.out.println("File not found."); } } } ```

This code reads a file named "filename.txt" line by line and prints each line to the console. If the file doesn't exist, it prints a user-friendly message.

Reading Alpha-Numeric Data with Scanner

Scanner's convenience extends to reading alpha-numeric data, revealing its true potential. Here's how to read integer and double values from a file:

```java import java.io.File; import java.io.IOException; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { try { Scanner sc = new Scanner(new File("filename.txt")); while (sc.hasNext()) { System.out.println("Integer: " + sc.nextInt()); System.out.println("Double: " + sc.nextDouble()); } sc.close(); } catch (IOException e) { System.out.println("File not found."); } } } ```

This code reads integer and double values from a file named "filename.txt" and prints them to the console. In this example, we assume that the file contains one integer and one double value per line.

Reading files in Java is a critical skill that every developer should possess. W3Schools offers a wealth of knowledge on file handling that every aspiring Java developer should tap into. As you've seen in this article, reading files in Java is a straightforward process with the right tools and knowledge. Now go forth and master file I/O operations to build more powerful applications!