Techoceanhub logo

File input/output operations

File input/output operations in Java involve reading from and writing to files on the system. The java.io package provides classes and methods to perform file operations. Here’s an overview of the basic file input/output operations in Java:

  1. Reading from a File:
    • Create a File object, specifying the path to the file.
    • Create a FileReader or BufferedReader object, passing the File object as a parameter.
    • Use the read() or readLine() method to read data from the file.
    • Close the file using the close() method when finished.

Example:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReadExample {
    public static void main(String[] args) {
        try {
            File file = new File("path/to/file.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }

            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


  1. Writing to a File:
    • Create a File object, specifying the path to the file.
    • Create a FileWriter or BufferedWriter object, passing the File object as a parameter.
    • Use the write() method to write data to the file.
    • Close the file using the close() method when finished.

Example:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteExample {
    public static void main(String[] args) {
        try {
            File file = new File("path/to/file.txt");
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            bufferedWriter.write("Hello, World!");
            bufferedWriter.newLine(); // Write a new line

            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Remember to handle exceptions appropriately when working with file input/output operations in Java, as they can occur if the file is not found, permissions are insufficient, or other issues arise.

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 .