Home RoadMap Blog Contact us Learn

6.Core Java Concepts: File Handling (I/O Streams)

Core Java Concepts: File Handling (I/O Streams)

File handling is a crucial part of Java for reading and writing data to and from files. Java provides several classes and methods to perform file operations efficiently. In this section, we will explore some of the most commonly used classes for I/O operations.

1. Reading and Writing Files

Java allows reading and writing files using various streams. Below are two important stream classes:

  • FileInputStream: For reading binary data from a file.
  • FileOutputStream: For writing binary data to a file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileHandlingExample {
    public static void main(String[] args) {
        try (FileOutputStream fos = new FileOutputStream("example.txt")) {
            String content = "Hello, File Handling!";
            fos.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (FileInputStream fis = new FileInputStream("example.txt")) {
            int i;
            while ((i = fis.read()) != -1) {
                System.out.print((char) i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. BufferedReader and BufferedWriter

The BufferedReader and BufferedWriter classes are used for efficient reading and writing of text files. They buffer characters, making reading and writing faster.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedReaderWriterExample {
    public static void main(String[] args) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("buffered_example.txt"))) {
            bw.write("BufferedWriter makes writing more efficient!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (BufferedReader br = new BufferedReader(new FileReader("buffered_example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Serialization and Deserialization

Serialization is the process of converting an object into a byte stream, while deserialization is the reverse process—converting a byte stream back into an object. This is especially useful when you need to store objects in files.

import java.io.*;

class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return name + " is " + age + " years old.";
    }
}

public class SerializationExample {
    public static void main(String[] args) {
        Person person = new Person("John", 30);

        // Serialization
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialization
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
            Person deserializedPerson = (Person) ois.readObject();
            System.out.println(deserializedPerson);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Quick Exercise: Try writing a program to read and write a text file using BufferedReader and BufferedWriter, and serialize an object to a file.

Conclusion

Understanding Java's file handling through streams and serialization can help in automating tasks like reading configurations or logging results in Selenium automation scripts. Efficient file management allows you to work with test data, logs, and reports.

File Handling in java

Recent Posts