Techoceanhub logo

Reading from and writing to files in java

In Java, you can read from and write to files using various classes from the java.io package. Here’s an example of how you can perform file reading and writing operations:

  • Reading from a File:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReadExample {
    public static void main(String[] args) {
        String fileName = "path/to/file.txt";
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, we use a BufferedReader to read the file line by line. We create an instance of FileReader by providing the file path and pass it to the BufferedReader constructor. Inside the try block, we read each line using readLine() method until it returns null, indicating the end of the file. The IOException is caught in case there is an error while reading the file.

  • Writing to a File:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteExample {
    public static void main(String[] args) {
        String fileName = "path/to/file.txt";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write("Hello, World!");
            writer.newLine();
            writer.write("This is a sample text.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we use a BufferedWriter to write data to a file. We create an instance of FileWriter by providing the file path and pass it to the BufferedWriter constructor. Inside the try block, we use the write() method to write data to the file. The newLine() method is used to insert a newline character. The IOException is caught in case there is an error while writing to the file.

Remember to replace "path/to/file.txt" with the actual file path you want to read from or write to. Additionally, it’s important to handle any exceptions that might occur during file operations using try-catch blocks to ensure proper error handling and resource cleanup.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .