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

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial