Object serialization To store an object, an array of objects, or some other complex structure: • Storing all the pieces of an object separately and reconstruct the object when that da
Trang 1Input/Output StreamChapter 7
Trang 2Outline
Trang 3Input/Output streams
destination
and write information to an output stream
Trang 4Standard I/O
There are three standard I/O streams:
• standard output – defined by System.out
• standard input – defined by System.in
• standard error – defined by System.err
statements
System.in typically represents keyboard input, which
we've used many times with Scanner objects
particular window on the monitor screen
Trang 5Exception in I/O
FileNotFoundException is thrown if the file cannot be
located (when the FileReader constructor is executed)
IOException
• A file might not exist
• Even if the file exists, a program may not be able to find it
• The file might not contain the kind of data we expect
Trang 6Outline
Trang 7Writing text files
Trang 8Writing text files
• write method: allows to write a content to a file
• newLine method: allows to break a line
Trang 9FileWriter fw = new FileWriter (" Lop.txt ");
BufferedWriter bw = new BufferedWriter (fw);
bw.write(" TCTH30A - Trung cap tin hoc 30A ");
bw.write(" TCTH30B - Trung cap tin hoc 30B ");
bw.write(" CDTH7K - Cao dang tin hoc 7K ");
bw.close();
}
}
Trang 10FileWriter fw = new FileWriter (" Lop.txt “);
BufferedWriter bw = new BufferedWriter (fw);
bw.write(" TCTH30A - Trung cap tin hoc 30A\r\n ");
bw.write(" TCTH30B - Trung cap tin hoc 30B\r\n ");
bw.write(" CDTH7K - Cao dang tin hoc 7K\r\n ");
bw.close();
Trang 11Reading text files
Trang 12Reading text files
which allows us to read an entire line of characters in one operation
• the readLine method returns a string
• the readLine method returns null when no more input is available
Trang 13FileReader fr = new FileReader (filename);
BufferedReader br = new BufferedReader (fr);
TCTH30A - Trung cap tin hoc 30A
TCTH30B - Trung cap tin hoc 30B
CDTH7K - Cao dang tin hoc 7K
Lop.txt
catch ( FileNotFoundException exception) {
System.out.println (" The file " +
filename + " was not found ");
Trang 14Writing - Reading text files
Trang 15FileOutputStream fis = new FileOutputStream (“LopHoc.txt”);
OutputStreamWriter isr = new OutputStreamWriter (fis, "UTF-8");
br.write(" Trung cấp tin học 30K ");
Trang 16FileInputStream fis = new FileInputStream (filename);
InputStreamReader isr = new InputStreamReader (fis, " UTF-8 ");
BufferedReader br = new BufferedReader (isr);
catch ( FileNotFoundException exception){
System.out.println (" The file " + filename + " was not found ");
}
catch ( IOException exception){
Trang 17Outline
Trang 18Object serialization
To store an object, an array of objects, or some other
complex structure:
• Storing all the pieces of an object separately and reconstruct the
object when that data is read back in more complex the
information, the more difficult
• Object serialization: represents an object as a sequence
of bytes that can be stored in a file or transferred to another
computer
Trang 19Serialization - Deserialization
Serialization refers to the process of saving the
state of an object by sending it to an output stream
object back into memory
Trang 20public final void writeObject (Object writeObject x ) throws IOException
public final Object readObject() throws readObject IOException, ClassNotFoundException
Trang 21public final Object readObject() throws readObject Exception
public final void writeObject (Object writeObject x ) throws Exception
Trang 22 We have the CountryInfo objects that we want to save to
a file
objects have been serialized and written out to a file, after that let’s deserialize them
Trang 23import java.io.Serializable;
public class CountryInfo implements Serializable
{
private String name, abbreviation, capitol;
private long area, population;
String result = "Name: " + name + "\n";
result += "Abbreviation: " + abbreviation + "\n";
result += "Capitol: " + capitol + "\n";
result += "Area: " + area + " square kilometers\n";
result += "Population: " + population + "\n";
result += "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
return result;
This class implements the Serializable interface
Trang 24Example programs
Write – Read a CountryInfo object
Write – Read an array of CountryInfo objects by separately
Write – Read an array object
Write – Read an ArrayList
Trang 25country = new CountryInfo (“ Viet Nam ", “ VIE ", “ Ha Noi ", 9629091L, 278058900L);
FileOutputStream file = new FileOutputStream (" countries.dat ");
// Serialize the object to a file
outStream writeObject (country);
Trang 26CountryInfo country = new CountryInfo() ;
FileInputStream file = new FileInputStream (" countries.dat ");
ObjectInputStream inStream = new ObjectInputStream (file);
// Deserialize the objects country = ( CountryInfo ) inStream readObject ();
inStream.close();
// Print the objects System.out.println (country); Read an object CountryInfo
Trang 27Output
Trang 28Example programs
Write – Read a CountryInfo object
Write – Read an array of CountryInfo objects by separately
Write – Read an array object
Write – Read an ArrayList
Trang 29CountryInfo[] countries = new CountryInfo[5];
countries[0] = new CountryInfo ("United States of America", "USA", "Washington, D.C.",
FileOutputStream file = newFileOutputStream ("countries.dat");
ObjectOutputStream outStream = new ObjectOutputStream (file);
// Serialize the objects to a filefor (int i = 0; i < countries.length; i++) outStream.writeObject (countries[i]);
outStream.close();
}
Write an object separately
Trang 30// Deserialize the objects
countries[i] = (CountryInfo) inStream.readObject();
inStream.close();
// Print the objects
Read an object separately
Trang 31// Deserialize the objects
countries[i] = (CountryInfo) inStream.readObject();
inStream.close();
// Print the objects
System.out.println (c);
}
Read an object separately
Trang 32Output
Trang 33Example programs
Write – Read a CountryInfo object
Write – Read an array of CountryInfo objects by separately
Write – Read an array object
Write – Read an ArrayList
Trang 34CountryInfo[] countries = new CountryInfo[5];
countries[0] = new CountryInfo ("United Statesof America", "USA", "Washington, D.C.",
FileOutputStream file = newFileOutputStream ("countries.dat");
ObjectOutputStream outStream = new ObjectOutputStream (file);
// Serialize the objects to a file
outStream.writeObject (countries);
Write an array of object
Trang 35FileInputStream file = new FileInputStream (" countries.dat ");
ObjectInputStream inStream = new ObjectInputStream (file);
// Deserialize the objects
inStream.close();
// Print the objects
for ( CountryInfo c : countries) System.out.println (c);
}
}
countries = ( CountryInfo []) inStream.readObject();
Read an array of object
Trang 36Output
Trang 37Example programs
Write – Read a CountryInfo object
Write – Read an array of CountryInfo objects by separately
Write – Read an array object
Write – Read an ArrayList
Trang 38Write – Read an ArrayList
The ArrayList class also implements the
Serializable interface, so we can store an entire list
of objects in one operation
If we had stored the CountryInfo objects in an ArrayList,
we should write the entire set of objects out in one
operation
Likewise, we should then read the entire ArrayList of
CountryInfo objects from the file in one operation
Keep in mind that the objects stored in the ArrayList must also implement the Serializable interface for this to work
Trang 39import java.io.*;
import java.util.ArrayList;
public class WriteCountryInfo3 {
public static void main (String[] args) throws IOException
arrCountries.add (new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L));
FileOutputStream file = newFileOutputStream ("countries.dat");
ObjectOutputStream outStream = new ObjectOutputStream (file);
// Serialize the object arraylist to a file
Trang 40ArrayList<CountryInfo> arrCountries = new ArrayList<CountryInfo>();
FileInputStream file = new FileInputStream ("countries.dat");
ObjectInputStream inStream = new ObjectInputStream (file);
// Deserialize the objects
arrCountries = (ArrayList) inStream.readObject();
inStream.close();
// Print the objects
System.out.println ("DOC MOT TAP CAC DOI TUONG VAO ARRAYLIST");
System.out.println ("+++++++++++++++++++++++++++++++++++++++++++++++++");
for (Object c : arrCountries)
Read an ArrayList object