A Portion of Java’s File ClassesObject InputStream OutputStream FileInputStream ObjectInputStream ObjectOutputStream FileOutputStream File These classes are included in the java.io packa
Trang 1Files and Serialization
Nguyen Thanh Phuoc
Trang 2Used to transfer data to and from secondary storage
Diskette Memory Input file
Output file
Trang 3A Portion of Java’s File Classes
Object
InputStream OutputStream
FileInputStream ObjectInputStream ObjectOutputStream FileOutputStream
File
These classes are included in the java.io package,
which must be imported into any application that uses
Trang 4Basic 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
Trang 5Basic 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
Trang 6Files and Exceptions
• Java requires the programmer to catch file exceptions
• For example, an IOException can be thrown during
at attempt to open a file
Trang 7Serializing 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
Trang 8Serializing 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
Trang 9import 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
Trang 10Writing 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
Trang 11Writing Objects to Output Files
new FileOutputStream(<a String>)
new ObjectOutputStream(<a FileOutputStream>)
<an ObjectOutputStream>.writeObject(<any object>)
A file name
Trang 12Writing 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();
Trang 13Reading 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
Trang 14Reading Objects from Input Files
new FileInputStream(<a String>)
new ObjectInputStream(<a FileInputStream>)
(<class name>) <an objectInputStream>.readObject()
A file name
Trang 15Reading 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());
}