Files and Serialization
Files and Serialization
Nguyen Thanh Phuoc
Files
Files
Used to transfer data to and from secondary storage
Diskette
Memory
Input file
Output file
A Portion of Java’s File Classes
A Portion of Java’s File Classes
Object
InputStream OutputStream
FileInputStream FileOutputStreamObjectInputStream ObjectOutputStream
File
These classes are included in the java.io package,
which must be imported into any application that uses
them
Basic Algorithm for File Output
Basic Algorithm for File Output
Open an output connection to a file
Write the data to the file
Close the output connection to the file
FileOutputStream
Disk
Basic Algorithm for File Input
Basic Algorithm for File Input
Open an input connection to a file
Read the data from the file and process it
Close the input connection to the file
FileInputStream
Disk
Files and Exceptions
Files and Exceptions
•
Java requires the programmer to catch file exceptions
•
For example, an IOException can be thrown during
at attempt to open a file
Serializing a Data Model
Serializing a Data Model
•
A data is said to be serialized when it can be
permanently saved on a storage medium
•
Serialization requires file management
•
Java support object serialization
Serializing a Data
Serializing a Data
•
All classes to be serialized must implement the
Serializable interface
•
This interface is defined in the java.io package
•
Many standard java classes are already serializable
import java.io.Serializable;
public class Employee implements Serializable{
private String name;
private int[] days;
public Employee(String name, int[] days){
this.name = name;
this.days = days;
}
}
Serializing the Employee System
Serializing the Employee System
Many of Java’s data classes are already serializable
All specialized data classes should be made to be serializable
Writing Objects to Output Files
Writing Objects to Output Files
•
Create a new instance of FileOutputStream
•
Wrap around this object a new instance of
ObjectOutputStream
•
Use the method writeObject to send an object out
on the stream
Writing Objects to Output Files
Writing Objects to Output Files
new FileOutputStream(<a String>)
new ObjectOutputStream(<a FileOutputStream>)
<an ObjectOutputStream>.writeObject(<any object>)
A file name
Writing Objects to Output Files
Writing Objects to Output Files
int[] days = {8, 8, 8, 10, 6};
Employee emp = new Employee("Bill", days);
try{
FileOutputStream fos = new
FileOutputStream("employee.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(emp);
fos.flush();
fos.close();
}catch(Exception e){
System.out.println("Error in output:" + e.toString());
}
Reading Objects from Input Files
Reading Objects from Input Files
•
Create a new instance of FileInputStream
•
Wrap around this object a new instance of
ObjectInputStream
•
Use the method readObject to receive an object from
the stream
•
Cast the received object to its actual class
Reading Objects from Input Files
Reading Objects from Input Files
new FileInputStream(<a String>)
new ObjectInputStream(<a FileInputStream>)
(<class name>) <an objectInputStream>.readObject()
A file name
Reading Objects from Input Files
Reading Objects from Input Files
Employee emp;
try{
FileInputStream fis = new FileInputStream("employee.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
emp = (Employee) ois.readObject();
fis.close();
}catch(Exception e){
System.out.println("Error in input:" + e.toString());
}