Java File I/O – Read and Write Files

πŸ“„ Java File I/O – Read and Write Files

Java provides powerful and flexible APIs for file input and output (I/O), allowing you to read from and write to files easily. File I/O in Java can be done using several classes from the java.io and java.nio.file packages.


✅ 1. Reading Files in Java

πŸ”Ή Using BufferedReader

java

Copy

Edit

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


public class ReadFileExample {

    public static void main(String[] args) {

        try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {

            String line;

            while ((line = reader.readLine()) != null) {

                System.out.println(line);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

πŸ”Ή Using Files.readAllLines (Java 8+)

java

Copy

Edit

import java.nio.file.Files;

import java.nio.file.Paths;

import java.io.IOException;

import java.util.List;


public class ReadFileWithNIO {

    public static void main(String[] args) {

        try {

            List<String> lines = Files.readAllLines(Paths.get("example.txt"));

            lines.forEach(System.out::println);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

✅ 2. Writing to Files in Java

πŸ”Ή Using BufferedWriter

java

Copy

Edit

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;


public class WriteFileExample {

    public static void main(String[] args) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {

            writer.write("Hello, Java File I/O!");

            writer.newLine();

            writer.write("This is a second line.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

πŸ”Ή Using Files.write (Java 8+)

java

Copy

Edit

import java.nio.file.Files;

import java.nio.file.Paths;

import java.io.IOException;

import java.util.Arrays;


public class WriteFileWithNIO {

    public static void main(String[] args) {

        try {

            Files.write(Paths.get("output.txt"), Arrays.asList("Line 1", "Line 2"));

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

🧠 Tips for File I/O in Java

Always close resources or use try-with-resources to prevent memory leaks.


Handle IOExceptions gracefully.


Use Paths.get("filename") instead of hard-coded paths for portability.


Prefer java.nio.file package for modern file operations (Java 7+).


πŸ§ͺ Bonus: Append to a File

java

Copy

Edit

try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt", true))) {

    writer.write("This line will be appended.");

    writer.newLine();

} catch (IOException e) {

    e.printStackTrace();

}

Learn Full Stack JAVA Course in Hyderabad

Read More

Multithreading and Concurrency in Java

Exception Handling in Java

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions 


Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know