Reading and Writing Files in Java

Tutorial 1 of 5

1. Introduction

In this tutorial, we will focus on how to read and write files in Java. This is an important aspect of Java programming as it allows you to interact with data stored in files. By the end of this tutorial, you will be able to read data from a file, write data to a file, and handle exceptions during these operations.

Prerequisites:
- Basic knowledge of Java programming.
- Familiarity with Java's exception handling.

2. Step-by-Step Guide

Java provides several classes to perform file operations. These classes can be found in the java.io package. The two main classes we will learn about in this tutorial are FileReader and FileWriter.

FileReader Class:
This class is used to read data from a file. It uses the default character encoding to read data.

FileWriter Class:
This class is used to write data to a file. Similar to FileReader, it uses the default character encoding to write data.

3. Code Examples

Example 1: Reading a File

import java.io.*;

public class ReadFile {
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader("test.txt"); // Specify the file name
            int character;
            while ((character = reader.read()) != -1) {
                System.out.print((char) character); // Cast to char and display it
            }
            reader.close(); // Always close the reader
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we're opening a file called test.txt and reading from it. We keep reading characters until there are none left (reader.read() returns -1 when the end of the file is reached). The read characters are then displayed to the console.

Example 2: Writing to a File

import java.io.*;

public class WriteFile {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("output.txt"); // Specify the file name
            writer.write("Hello, World!"); // Write to the file
            writer.close(); // Always close the writer
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we're creating a file called output.txt and writing "Hello, World!" to it. It's crucial to close the FileWriter after we're done to prevent any resource leaks.

4. Summary

In this tutorial, we have learnt how to read from and write to files in Java using FileReader and FileWriter classes. These are fundamental skills that you will find useful in many programming scenarios.

5. Practice Exercises

Exercise 1: Write a Java program to read a text file and print each line to the console.

Exercise 2: Write a Java program to write the numbers 1 to 10 into a text file, each on a new line.

Exercise 3: Write a Java program to copy the content of one text file to another.

Remember to try these exercises on your own before looking at the solutions. With practice, you will become more comfortable with these concepts.

6. Further Resources

For more detailed information on file handling in Java, check out the official Java documentation: Java IO.