Objects File I/O
in JAVA
Part-2
Instructor Name: Samia Zaffar
Department of Computer Science, COMSATS University Islamabad, Wah Campus, Pakistan
Outline
2
ObjectOutputSteam
ObjectInputStream
Serialization
Write/Read objects in File
List of objects Write/Read in File
ObjectOutputStream
• • Used to write objects to a file
• • Requires classes to implement Serializable
Example: Writing an Object
• ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("[Link]"));
• [Link](obj);
• [Link]();
ObjectInputStream
• • Used to read objects from a file
• • Must cast object after reading
Example: Reading an Object
• ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("[Link]"));
• MyClass obj = (MyClass) [Link]();
• [Link]();
Why we need Serialization
• Serialization means converting an object into a
stream of bytes
so that it can be:
saved to a file
sent over a network
stored in a database
transferred between programs
recreated later by deserialization
• Serialization lets objects become portable
Serializable interface
• The Serializable Interface is a Marker Interface
• It has no methods.
• It only acts like a “permission slip”
• It tells the JVM:
“this class can be serialized.”
• If a class implements Serializable, then Java knows it
is safe to serialize the object.
• If you try to serialize a class that does NOT implement
Serializable →
Java throws: [Link]
Write/Read Book Object Using Serialization
import [Link].*;
// ---------- BOOK CLASS ----------
class Book implements Serializable {
private String title;
private String author;
private double price;
// Constructor
public Book(String title, String author, double price) {
[Link] = title;
[Link] = author;
[Link] = price;
}
// Display method
public void display() {
[Link]("Title: " + title);
[Link]("Author: " + author);
[Link]("Price: " + price);
}
}
Write/Read Book Object Using Serialization
// ---------- MAIN CLASS ----------
public class BookIOExample {
public static void main(String[] args) { String filename = "[Link]";
// Create a Book object
Book b1 = new Book("Harry Potter", "J. K. Rowling", 2500);
// ----- WRITE OBJECT TO FILE -----
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
[Link](b1);
[Link]();
[Link]("Book object written to file successfully.\n");
}
catch (IOException e) {
[Link]();
}
Write/Read Book Object Using Serialization
// ----- READ OBJECT FROM FILE -----
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
Book b2 = (Book) [Link](); // type cast needed
[Link]();
[Link]("Book object read from file:\n");
[Link]();
}
catch (IOException | ClassNotFoundException e) {
[Link]();
}
}
}
Read/Write ArrayList in File
• • For multiple books create an ArrayList of books and write in a file in one step.
String filename = "[Link]";
// -------- CREATE ARRAYLIST --------
ArrayList<Book> bookList = new ArrayList<>();
[Link](new Book("Object-oriented Programming", "Robert Lafore", 2500));
[Link](new Book("Introduction to Java Programming", "[Link] Liang", 1800));
[Link](new Book("C++ How to Program", "Dietal", 1200));
// -------- WRITE ARRAYLIST TO FILE --------
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
[Link](bookList);
[Link]();
[Link]("ArrayList<Book> written to file successfully!\n");
} catch (IOException e) {
[Link]();
}
Read/Write ArrayList in File
• • For multiple books read from file in an ArrayList of books.
// -------- READ ARRAYLIST FROM FILE --------
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
ArrayList<Book> readList = (ArrayList<Book>) [Link]();
[Link]();
[Link]("Reading ArrayList<Book> from file:\n");
// Display each book
for (Book b : readList) {
[Link]();
}
} catch (IOException | ClassNotFoundException e) {
[Link]();
}