Reading and Writing Files in Java


File handling in Java allows us to read, write, and manipulate files. This tutorial will guide you through the process step-by-step.

Step 1: Writing to a File

Below is an example of writing text to a file using the FileWriter class:

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class WriteToFileExample {
        public static void main(String[] args) {
            try {
                FileWriter writer = new FileWriter("output.txt");
                writer.write("Hello, this is a test file.\n");
                writer.write("Java makes file handling easy.\n");
                writer.close();
                System.out.println("File written successfully.");
            } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
            }
        }
    }
        

Step 2: Reading from a File

Below is an example of reading text from a file using the BufferedReader class:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFromFileExample {
        public static void main(String[] args) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader("output.txt"));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                reader.close();
            } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
            }
        }
    }
        

Step 3: Using the Files Class

The Files class in the java.nio.file package provides simpler methods for file handling.

Writing to a File:

    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.io.IOException;
    
    public class FilesWriteExample {
        public static void main(String[] args) {
            String content = "This is written using Files.write method.";
            try {
                Files.write(Paths.get("output2.txt"), content.getBytes());
                System.out.println("File written successfully.");
            } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
            }
        }
    }
        

Reading from a File:

    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.io.IOException;
    import java.util.List;
    
    public class FilesReadExample {
        public static void main(String[] args) {
            try {
                List lines = Files.readAllLines(Paths.get("output2.txt"));
                for (String line : lines) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
            }
        }
    }
        

Summary

In this tutorial, you learned:

  • How to write to a file using FileWriter
  • How to read from a file using BufferedReader
  • How to use the Files class for simplified file handling

Choose the method that best fits your requirements and start working with files in Java!





Advertisement