Java File Handling, Serialization & Deserialization: Complete Guide
Java provides powerful APIs for file handling and object persistence through serialization and deserialization. These concepts are essential for storing data permanently and transferring objects between systems.
What is File Handling in Java?
File handling allows Java programs to create, read, write, and delete files.
Java provides classes in the java.io package for file operations.
- Store data permanently
- Read and write files
- Transfer data between applications
File Class in Java
The File class is used to represent files and directories.
Example
import java.io.File;
class Test {
public static void main(String[] args) {
File f = new File("test.txt");
System.out.println(f.exists());
System.out.println(f.getName());
}
}
Creating a File
File f = new File("data.txt");
f.createNewFile();
File Input and Output Streams
Java uses streams to read and write data.
1. FileOutputStream (Write)
import java.io.*;
class Test {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("data.txt");
fos.write(65); // ASCII value
fos.close();
}
}
2. FileInputStream (Read)
FileInputStream fis = new FileInputStream("data.txt");
int data = fis.read();
System.out.println(data);
fis.close();
Character Streams
Used for reading and writing text data.
Example
FileWriter fw = new FileWriter("file.txt");
fw.write("Hello Java");
fw.close();
FileReader fr = new FileReader("file.txt");
int ch = fr.read();
System.out.println((char)ch);
fr.close();
Serialization in Java
Serialization is the process of converting an object into a byte stream so that it can be saved to a file.
Steps
- Class must implement
Serializable - Use
ObjectOutputStream
Example
import java.io.*;
class Student implements Serializable {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
}
class Test {
public static void main(String[] args) throws Exception {
Student s = new Student(1, "Java");
ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream("student.ser"));
oos.writeObject(s);
oos.close();
}
}
Deserialization in Java
Deserialization is the process of converting a byte stream back into an object.
Example
import java.io.*;
class Test {
public static void main(String[] args) throws Exception {
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream("student.ser"));
Student s = (Student) ois.readObject();
System.out.println(s.id + " " + s.name);
ois.close();
}
}
transient Keyword
The transient keyword is used to skip variables during serialization.
class Test implements Serializable {
transient int password;
}
File Handling Summary
- File class → represents files
- Streams → read/write data
- Serialization → object to file
- Deserialization → file to object
File Handling FAQ
What is serialization?
Converting object into byte stream.
What is deserialization?
Converting byte stream back to object.
Why Serializable interface is used?
To mark class eligible for serialization.