12 Advanced I/O Streams
Topics <ul><li>General Stream Types </li></ul><ul><ul><li>Character and Byte Streams </li></ul></ul><ul><ul><li>Input and ...
Topics <ul><li>Writer  Classes </li></ul><ul><ul><li>Writer  Methods </li></ul></ul><ul><ul><li>Node  Writer  Classes </li...
Topics <ul><li>OutputStream  Classes </li></ul><ul><ul><li>OutputStream  Methods </li></ul></ul><ul><ul><li>Node  OutputSt...
General Stream Types <ul><li>Streams </li></ul><ul><ul><li>Abstraction of a file or a device that allows a series of items...
Character and Byte Streams <ul><li>Character streams </li></ul><ul><ul><li>File or device abstractions for Unicode charact...
Input and Output Streams <ul><li>Input or source streams </li></ul><ul><ul><li>Can read from these streams </li></ul></ul>...
Node and Filter Streams <ul><li>Node streams </li></ul><ul><ul><li>Contain the basic functionality of reading or writing f...
The  File  Class <ul><li>Not a stream class </li></ul><ul><li>Important since stream classes manipulate  File  objects </l...
The  File  Class: Constructors <ul><li>Has four constructors </li></ul>
The  File  Class: Methods
The  File  Class: Methods
The  File  Class: Example <ul><li>import java.io.*; </li></ul><ul><li>public class FileInfoClass { </li></ul><ul><li>publi...
The  File  Class: Example <ul><li>/* Create a temporary directory instead. */ </li></ul><ul><li>System.out.println(&quot;C...
The  File  Class: Example <ul><li>System.out.println(fileName + </li></ul><ul><li>(fn.exists()? &quot;exists&quot;: &quot;...
The  File  Class: Example <ul><li>for (int i = 0; i < content.length; i++) { </li></ul><ul><li>System.out.println(content[...
The  File  Class: Example <ul><li>System.out.println(fileName + &quot; is &quot; + fn.length() </li></ul><ul><li>+ &quot; ...
The  Reader  Class: Methods
The  Reader  Class: Methods
Node  Reader  Classes
Filter  Reader  Classes
The  Writer  Class: Methods
Node  Writer  Classes
Filter  Writer  Classes
Basic  Reader / Writer  Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void copy(S...
Basic  Reader / Writer  Example <ul><li>while ((data =  reader.read() ) != -1) { </li></ul><ul><li>writer.write(data); </l...
Basic  Reader / Writer  Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = args...
Modified  Reader / Writer  Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void cop...
Modified  Reader / Writer  Example <ul><li>while ((data =  reader.readLine() ) != null) { </li></ul><ul><li>writer.write(d...
Modified  Reader / Writer  Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = a...
The  InputStream  Class: Methods
The  InputStream  Class: Methods
Node  InputStream  Classes
Filter  InputStream  Classes
The  OutputStream  Class: Methods
Node  OutputStream  Classes
Filter  OutputStream  Classes
Basic  InputStream /  OutputStream  Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li...
Basic  InputStream /  OutputStream  Example <ul><li>while ((data =  inputStr.read() ) != -1) { </li></ul><ul><li>outputStr...
Basic  InputStream /  OutputStream  Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inpu...
Modified  InputStream /  OutputStream  Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul>...
Modified  InputStream /  OutputStream  Example <ul><li>while ((data =  inputStr.read() ) != -1) { </li></ul><ul><li>output...
Modified  InputStream /  OutputStream  Example <ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); ...
Serialization <ul><li>Definition: </li></ul><ul><ul><li>Supported by the Java Virtual Machine (JVM) </li></ul></ul><ul><ul...
Serialization <ul><li>Streams for serialization </li></ul><ul><ul><li>ObjectInputStream </li></ul></ul><ul><ul><ul><li>For...
Non-Serializable Objects <ul><li>When an object is serialized: </li></ul><ul><ul><li>Only the object's data are preserved ...
The  transient  Keyword <ul><li>A class containing a non-serializable object can still be serialized </li></ul><ul><ul><li...
Serialization: Writing an Object Stream <ul><li>Use the  ObjectOutputStream  class </li></ul><ul><li>Use its  writeObject ...
Serialization: Writing an Object Stream <ul><li>import java.io.*; </li></ul><ul><li>public class SerializeBoolean { </li><...
Serialization: Writing an Object Stream <ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></...
Deserialization: Reading an Object Stream <ul><li>Use the  ObjectInputStream  class </li></ul><ul><li>Use its  readObject ...
Deserialization: Reading an Object Stream <ul><li>import java.io.*; </li></ul><ul><li>public class UnserializeBoolean { </...
Deserialization: Reading an Object Stream <ul><li>} catch (Exception e) { </li></ul><ul><li>e.printStackTrace(); </li></ul...
Deserialization: Reading an Object Stream <ul><li>public static void main(String args[]) { </li></ul><ul><li>UnserializeBo...
Summary <ul><li>General Stream Types </li></ul><ul><ul><li>Character and Byte Streams </li></ul></ul><ul><ul><li>Input and...
Summary <ul><li>Reader  Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>read ,  close ,  mark ,  mark...
Summary <ul><li>Writer  Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>write ,  close ,  flush </li>...
Summary <ul><li>InputStream  Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>read ,  close ,  mark , ...
Summary <ul><li>OutputStream  Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>write ,  close ,  flush...
Summary <ul><li>Serialization </li></ul><ul><ul><li>Definition </li></ul></ul><ul><ul><li>The  transient  Keyword </li></u...
Upcoming SlideShare
Loading in …5
×

Jedi Slides Intro2 Chapter12 Advanced Io Streams

2,327 views

Published on

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,327
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
161
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Jedi Slides Intro2 Chapter12 Advanced Io Streams

  1. 1. 12 Advanced I/O Streams
  2. 2. Topics <ul><li>General Stream Types </li></ul><ul><ul><li>Character and Byte Streams </li></ul></ul><ul><ul><li>Input and Output Streams </li></ul></ul><ul><ul><li>Node and Filter Streams </li></ul></ul><ul><li>The File Class </li></ul><ul><li>Reader Classes </li></ul><ul><ul><li>Reader Methods </li></ul></ul><ul><ul><li>Node Reader Classes </li></ul></ul><ul><ul><li>Filter Reader Classes </li></ul></ul>
  3. 3. Topics <ul><li>Writer Classes </li></ul><ul><ul><li>Writer Methods </li></ul></ul><ul><ul><li>Node Writer Classes </li></ul></ul><ul><ul><li>Filter Writer Classes </li></ul></ul><ul><li>InputStream Classes </li></ul><ul><ul><li>InputStream Methods </li></ul></ul><ul><ul><li>Node InputStream Classes </li></ul></ul><ul><ul><li>Filter InputStream Classes </li></ul></ul>
  4. 4. Topics <ul><li>OutputStream Classes </li></ul><ul><ul><li>OutputStream Methods </li></ul></ul><ul><ul><li>Node OutputStream Classes </li></ul></ul><ul><ul><li>Filter OutputStream Classes </li></ul></ul><ul><li>Serialization </li></ul><ul><ul><li>The transient Keyword </li></ul></ul><ul><ul><li>Serialization: Writing an Object Stream </li></ul></ul><ul><ul><li>Deserialization: Reading an Object Stream </li></ul></ul>
  5. 5. General Stream Types <ul><li>Streams </li></ul><ul><ul><li>Abstraction of a file or a device that allows a series of items to be read or written </li></ul></ul><ul><li>General Stream Categories </li></ul><ul><ul><li>Character and Byte Streams </li></ul></ul><ul><ul><li>Input and Output Streams </li></ul></ul><ul><ul><li>Node and Filter Streams </li></ul></ul>
  6. 6. Character and Byte Streams <ul><li>Character streams </li></ul><ul><ul><li>File or device abstractions for Unicode characters </li></ul></ul><ul><ul><li>Superclass of all classes for character streams: </li></ul></ul><ul><ul><ul><li>The Reader class </li></ul></ul></ul><ul><ul><ul><li>The Writer class </li></ul></ul></ul><ul><ul><ul><li>Both classes are abstract </li></ul></ul></ul><ul><li>Byte streams </li></ul><ul><ul><li>For binary data </li></ul></ul><ul><ul><li>Root classes for byte streams: </li></ul></ul><ul><ul><ul><li>The InputStream Class </li></ul></ul></ul><ul><ul><ul><li>The OutputStream Class </li></ul></ul></ul><ul><ul><ul><li>Both classes are abstract </li></ul></ul></ul>
  7. 7. Input and Output Streams <ul><li>Input or source streams </li></ul><ul><ul><li>Can read from these streams </li></ul></ul><ul><ul><li>Superclasses of all input streams: </li></ul></ul><ul><ul><ul><li>The InputStream Class </li></ul></ul></ul><ul><ul><ul><li>The Reader Class </li></ul></ul></ul><ul><li>Output or sink streams </li></ul><ul><ul><li>Can write to these streams </li></ul></ul><ul><ul><li>Root classes of all output streams: </li></ul></ul><ul><ul><ul><li>The OutputStream Class </li></ul></ul></ul><ul><ul><ul><li>The Writer Class </li></ul></ul></ul>
  8. 8. Node and Filter Streams <ul><li>Node streams </li></ul><ul><ul><li>Contain the basic functionality of reading or writing from a specific location </li></ul></ul><ul><ul><li>Types of node streams include files, memory and pipes </li></ul></ul><ul><li>Filter streams </li></ul><ul><ul><li>Layered onto node streams between threads or processes </li></ul></ul><ul><ul><li>For additional functionalities </li></ul></ul><ul><ul><li>Adding layers to a node stream is called stream chaining </li></ul></ul>
  9. 9. The File Class <ul><li>Not a stream class </li></ul><ul><li>Important since stream classes manipulate File objects </li></ul><ul><li>Abstract representation of actual files and directory pathnames </li></ul>
  10. 10. The File Class: Constructors <ul><li>Has four constructors </li></ul>
  11. 11. The File Class: Methods
  12. 12. The File Class: Methods
  13. 13. The File Class: Example <ul><li>import java.io.*; </li></ul><ul><li>public class FileInfoClass { </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>String fileName = args[0]; </li></ul><ul><li>File fn = new File(fileName); </li></ul><ul><li>System.out.println(&quot;Name: &quot; + fn.getName()); </li></ul><ul><li>if (!fn.exists()) { </li></ul><ul><li>System.out.println(fileName </li></ul><ul><li>+ &quot; does not exists.&quot;); </li></ul><ul><li>//continued... </li></ul>
  14. 14. The File Class: Example <ul><li>/* Create a temporary directory instead. */ </li></ul><ul><li>System.out.println(&quot;Creating temp </li></ul><ul><li>directory...&quot;); </li></ul><ul><li>fileName = &quot;temp&quot;; </li></ul><ul><li>fn = new File(fileName); </li></ul><ul><li>fn.mkdir(); </li></ul><ul><li>System.out.println(fileName + </li></ul><ul><li>(fn.exists()? &quot;exists&quot;: &quot;does not exist&quot;)); </li></ul><ul><li>System.out.println(&quot;Deleting temp </li></ul><ul><li>directory...&quot;); </li></ul><ul><li>fn.delete(); </li></ul><ul><li>//continued... </li></ul>
  15. 15. The File Class: Example <ul><li>System.out.println(fileName + </li></ul><ul><li>(fn.exists()? &quot;exists&quot;: &quot;does not exist&quot;)); </li></ul><ul><li>return; </li></ul><ul><li>} //end of: if (!fn.exists()) </li></ul><ul><li>System.out.println(fileName + &quot; is a &quot; + </li></ul><ul><li>(fn.isFile()? &quot;file.&quot; :&quot;directory.&quot;)); </li></ul><ul><li>if (fn.isDirectory()) { </li></ul><ul><li>String content[] = fn.list(); </li></ul><ul><li>System.out.println(&quot;The content of this </li></ul><ul><li>directory:&quot;); </li></ul><ul><li>//continued... </li></ul>
  16. 16. The File Class: Example <ul><li>for (int i = 0; i < content.length; i++) { </li></ul><ul><li>System.out.println(content[i]); </li></ul><ul><li>} </li></ul><ul><li>} //end of: if (fn.isDirectory()) </li></ul><ul><li>if (!fn.canRead()) { </li></ul><ul><li>System.out.println(fileName </li></ul><ul><li>+ &quot; is not readable.&quot;); </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>//continued... </li></ul>
  17. 17. The File Class: Example <ul><li>System.out.println(fileName + &quot; is &quot; + fn.length() </li></ul><ul><li>+ &quot; bytes long.&quot;); </li></ul><ul><li>System.out.println(fileName + &quot; is &quot; + </li></ul><ul><li>fn.lastModified() + &quot; bytes long.&quot;); </li></ul><ul><li>if (!fn.canWrite()) { </li></ul><ul><li>System.out.println(fileName </li></ul><ul><li>+ &quot; is not writable.&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. The Reader Class: Methods
  19. 19. The Reader Class: Methods
  20. 20. Node Reader Classes
  21. 21. Filter Reader Classes
  22. 22. The Writer Class: Methods
  23. 23. Node Writer Classes
  24. 24. Filter Writer Classes
  25. 25. Basic Reader / Writer Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void copy(String input, String output) { </li></ul><ul><li>FileReader reader; </li></ul><ul><li>FileWriter writer; </li></ul><ul><li>int data; </li></ul><ul><li>try { </li></ul><ul><li>reader = new FileReader(input); </li></ul><ul><li>writer = new FileWriter(output); </li></ul><ul><li>//continued... </li></ul>
  26. 26. Basic Reader / Writer Example <ul><li>while ((data = reader.read() ) != -1) { </li></ul><ul><li>writer.write(data); </li></ul><ul><li>} </li></ul><ul><li>reader.close(); </li></ul><ul><li>writer.close(); </li></ul><ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//continued... </li></ul>
  27. 27. Basic Reader / Writer Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = args[0]; </li></ul><ul><li>String outputFile = args[1]; </li></ul><ul><li>CopyFile cf = new CopyFile(); </li></ul><ul><li>cf.copy(inputFile, outputFile); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  28. 28. Modified Reader / Writer Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void copy(String input, String output) { </li></ul><ul><li>BufferedReader reader; </li></ul><ul><li>BufferedWriter writer; </li></ul><ul><li>String data; </li></ul><ul><li>try { </li></ul><ul><li>reader = new </li></ul><ul><li>BufferedReader(new FileReader(input)); </li></ul><ul><li>writer = new </li></ul><ul><li>BufferedWriter(new FileWriter(output)); </li></ul><ul><li>//continued... </li></ul>
  29. 29. Modified Reader / Writer Example <ul><li>while ((data = reader.readLine() ) != null) { </li></ul><ul><li>writer.write(data, 0, data.length); </li></ul><ul><li>} </li></ul><ul><li>reader.close(); </li></ul><ul><li>writer.close(); </li></ul><ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//continued... </li></ul>
  30. 30. Modified Reader / Writer Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = args[0]; </li></ul><ul><li>String outputFile = args[1]; </li></ul><ul><li>CopyFile cf = new CopyFile(); </li></ul><ul><li>cf.copy(inputFile, outputFile); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  31. 31. The InputStream Class: Methods
  32. 32. The InputStream Class: Methods
  33. 33. Node InputStream Classes
  34. 34. Filter InputStream Classes
  35. 35. The OutputStream Class: Methods
  36. 36. Node OutputStream Classes
  37. 37. Filter OutputStream Classes
  38. 38. Basic InputStream / OutputStream Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void copy(String input, String output) { </li></ul><ul><li>FileInputStream inputStr; </li></ul><ul><li>FileOutputStream outputStr; </li></ul><ul><li>int data; </li></ul><ul><li>try { </li></ul><ul><li>inputStr = new FileInputStream(input); </li></ul><ul><li>outputStr = new FileOutputStream(output); </li></ul><ul><li>//continued... </li></ul>
  39. 39. Basic InputStream / OutputStream Example <ul><li>while ((data = inputStr.read() ) != -1) { </li></ul><ul><li>outputStr.write(data); </li></ul><ul><li>} </li></ul><ul><li>inputStr.close(); </li></ul><ul><li>outputStr.close(); </li></ul><ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//continued... </li></ul>
  40. 40. Basic InputStream / OutputStream Example <ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = args[0]; </li></ul><ul><li>String outputFile = args[1]; </li></ul><ul><li>CopyFile cf = new CopyFile(); </li></ul><ul><li>cf.copy(inputFile, outputFile); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  41. 41. Modified InputStream / OutputStream Example <ul><li>import java.io.*; </li></ul><ul><li>class CopyFile { </li></ul><ul><li>void copy(String input) { </li></ul><ul><li>PushbackInputStream inputStr; </li></ul><ul><li>PrintStream outputStr; </li></ul><ul><li>int data; </li></ul><ul><li>try { </li></ul><ul><li>inputStr = new PushbackInputStream(new </li></ul><ul><li>FileInputStream(input)); </li></ul><ul><li>outputStr = new PrintStream(System.out); </li></ul><ul><li>//continued... </li></ul>
  42. 42. Modified InputStream / OutputStream Example <ul><li>while ((data = inputStr.read() ) != -1) { </li></ul><ul><li>outputStr.println(&quot;read data: &quot; + </li></ul><ul><li>(char) data); </li></ul><ul><li>inputStr.unread(data); </li></ul><ul><li>data = inputStr.read(); </li></ul><ul><li>outputStr.println(&quot;unread data: &quot; + </li></ul><ul><li>(char) data); </li></ul><ul><li>} </li></ul><ul><li>inputStr.close(); </li></ul><ul><li>outputStr.close(); </li></ul><ul><li>//continued... </li></ul>
  43. 43. Modified InputStream / OutputStream Example <ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>String inputFile = args[0]; </li></ul><ul><li>CopyFile cf = new CopyFile(); </li></ul><ul><li>cf.copy(inputFile); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  44. 44. Serialization <ul><li>Definition: </li></ul><ul><ul><li>Supported by the Java Virtual Machine (JVM) </li></ul></ul><ul><ul><li>Ability to read or write an object to a stream </li></ul></ul><ul><ul><li>Process of &quot;flattening&quot; an object </li></ul></ul><ul><ul><li>Goal: To save object to some permanent storage or to pass on to another object via the OutputStream class </li></ul></ul><ul><li>Writing an object: </li></ul><ul><ul><li>Its state should be written in a serialized form such that the object can be reconstructed as it is being read </li></ul></ul><ul><li>Persistence </li></ul><ul><ul><li>Saving an object to some type of permanent storage </li></ul></ul>
  45. 45. Serialization <ul><li>Streams for serialization </li></ul><ul><ul><li>ObjectInputStream </li></ul></ul><ul><ul><ul><li>For deserializing </li></ul></ul></ul><ul><ul><li>ObjectOutputStream </li></ul></ul><ul><ul><ul><li>For serializing </li></ul></ul></ul><ul><li>To allow an object to be serializable: </li></ul><ul><ul><li>Its class should implement the Serializable interface </li></ul></ul><ul><ul><li>Its class should also provide a default constructor or a constructor with no arguments </li></ul></ul><ul><ul><li>Serializability is inherited </li></ul></ul><ul><ul><ul><li>Don't have to implement Serializable on every class </li></ul></ul></ul><ul><ul><ul><li>Can just implement Serializable once along the class heirarchy </li></ul></ul></ul>
  46. 46. Non-Serializable Objects <ul><li>When an object is serialized: </li></ul><ul><ul><li>Only the object's data are preserved </li></ul></ul><ul><ul><li>Methods and constructors are not part of the serialized stream </li></ul></ul><ul><li>Some objects are not serializable </li></ul><ul><ul><li>Because the data they represent constantly changes </li></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>FileInputStream objects </li></ul></ul></ul><ul><ul><ul><li>Thread objects </li></ul></ul></ul><ul><li>A NotSerializableException is thrown if the serialization fails </li></ul>
  47. 47. The transient Keyword <ul><li>A class containing a non-serializable object can still be serialized </li></ul><ul><ul><li>Reference to non-serializable object is marked with the transient keyword </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>class MyClass implements Serializable { </li></ul></ul></ul><ul><ul><ul><li>transient Thread thread; </li></ul></ul></ul><ul><ul><ul><li>//try removing transient </li></ul></ul></ul><ul><ul><ul><li>int data; </li></ul></ul></ul><ul><ul><ul><li>/* some other data */ </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>The transient keyword prevents the data from being serialized </li></ul></ul>
  48. 48. Serialization: Writing an Object Stream <ul><li>Use the ObjectOutputStream class </li></ul><ul><li>Use its writeObject method </li></ul><ul><li>public final void writeObject(Object obj) </li></ul><ul><li>throws IOException </li></ul><ul><li>where, </li></ul><ul><ul><li>obj is the object to be written to the stream </li></ul></ul>
  49. 49. Serialization: Writing an Object Stream <ul><li>import java.io.*; </li></ul><ul><li>public class SerializeBoolean { </li></ul><ul><li>SerializeBoolean() { </li></ul><ul><li>Boolean booleanData = new Boolean(&quot;true&quot;); </li></ul><ul><li>try { </li></ul><ul><li>FileOutputStream fos = new </li></ul><ul><li>FileOutputStream(&quot;boolean.ser&quot;); </li></ul><ul><li>ObjectOutputStream oos = new </li></ul><ul><li>ObjectOutputStream(fos); </li></ul><ul><li>oos.writeObject(booleanData); </li></ul><ul><li>oos.close(); </li></ul><ul><li>//continued... </li></ul>
  50. 50. Serialization: Writing an Object Stream <ul><li>} catch (IOException ie) { </li></ul><ul><li>ie.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>SerializeBoolean sb = new SerializeBoolean(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  51. 51. Deserialization: Reading an Object Stream <ul><li>Use the ObjectInputStream class </li></ul><ul><li>Use its readObject method </li></ul><ul><li>public final Object readObject() </li></ul><ul><li>throws IOException, ClassNotFoundException </li></ul><ul><li>where, </li></ul><ul><ul><li>obj is the object to be read from the stream </li></ul></ul><ul><li>The Object type returned should be typecasted to the appropriate class name before methods on that class can be executed </li></ul>
  52. 52. Deserialization: Reading an Object Stream <ul><li>import java.io.*; </li></ul><ul><li>public class UnserializeBoolean { </li></ul><ul><li>UnserializeBoolean() { </li></ul><ul><li>Boolean booleanData = null; </li></ul><ul><li>try { </li></ul><ul><li>FileInputStream fis = new </li></ul><ul><li>FileInputStream(&quot;boolean.ser&quot;); </li></ul><ul><li>ObjectInputStream ois = new </li></ul><ul><li>ObjectInputStream(fis); </li></ul><ul><li>booleanData = (Boolean) ois.readObject(); </li></ul><ul><li>ois.close(); </li></ul><ul><li>//continued... </li></ul>
  53. 53. Deserialization: Reading an Object Stream <ul><li>} catch (Exception e) { </li></ul><ul><li>e.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>System.out.println(&quot;Unserialized Boolean from &quot; </li></ul><ul><li>+ &quot;boolean.ser&quot;); </li></ul><ul><li>System.out.println(&quot;Boolean data: &quot; + </li></ul><ul><li>booleanData ); </li></ul><ul><li>System.out.println(&quot;Compare data with true: &quot; + </li></ul><ul><li>booleanData.equals(new Boolean(&quot;true&quot;)) ); </li></ul><ul><li>} </li></ul><ul><li>//continued... </li></ul>
  54. 54. Deserialization: Reading an Object Stream <ul><li>public static void main(String args[]) { </li></ul><ul><li>UnserializeBoolean usb = </li></ul><ul><li>new UnserializeBoolean(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  55. 55. Summary <ul><li>General Stream Types </li></ul><ul><ul><li>Character and Byte Streams </li></ul></ul><ul><ul><li>Input and Output Streams </li></ul></ul><ul><ul><li>Node and Filter Streams </li></ul></ul><ul><li>The File Class </li></ul><ul><ul><li>Constructor </li></ul></ul><ul><li>File(String pathname) </li></ul><ul><ul><li>Methods </li></ul></ul>
  56. 56. Summary <ul><li>Reader Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>read , close , mark , markSupported , reset </li></ul></ul></ul><ul><ul><li>Node Reader Classes </li></ul></ul><ul><ul><ul><li>FileReader , CharArrayReader , StringReader , PipedReader </li></ul></ul></ul><ul><ul><li>Filter Reader Classes </li></ul></ul><ul><ul><ul><li>BufferedReader , FilterReader , InputStreamReader , </li></ul></ul></ul><ul><li>LineNumberReader , PushbackReader </li></ul>
  57. 57. Summary <ul><li>Writer Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>write , close , flush </li></ul></ul></ul><ul><ul><li>Node Writer Classes </li></ul></ul><ul><ul><ul><li>FileWriter , CharArrayWriter , StringWriter , PipedWriter </li></ul></ul></ul><ul><ul><li>Filter Writer Classes </li></ul></ul><ul><ul><ul><li>BufferedWriter , FilterWriter , OutputStreamWriter , PrintWriter </li></ul></ul></ul>
  58. 58. Summary <ul><li>InputStream Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>read , close , mark , markSupported , reset </li></ul></ul></ul><ul><ul><li>Node InputStream Classes </li></ul></ul><ul><ul><ul><li>FileInputStream , BufferedArrayInputStream , PipedInputStream </li></ul></ul></ul><ul><ul><li>Filter InputStream Classes </li></ul></ul><ul><ul><ul><li>BufferedInputStream , FilterInputStream , ObjectInputStream , DataInputStream , LineNumberInputStream , PushbackInputStream </li></ul></ul></ul>
  59. 59. Summary <ul><li>OutputStream Classes </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>write , close , flush </li></ul></ul></ul><ul><ul><li>Node OutputStream Classes </li></ul></ul><ul><ul><ul><li>FileOutputStream , BufferedArrayOutputStream , PipedOutputStream </li></ul></ul></ul><ul><ul><li>Filter OutputStream Classes </li></ul></ul><ul><ul><ul><li>BufferedOutputStream , FilterOutputStream , ObjectOutputStream , DataOutputStream , PrintStream </li></ul></ul></ul>
  60. 60. Summary <ul><li>Serialization </li></ul><ul><ul><li>Definition </li></ul></ul><ul><ul><li>The transient Keyword </li></ul></ul><ul><ul><li>Serialization: Writing an Object Stream </li></ul></ul><ul><ul><ul><li>Use the ObjectOutputStream class </li></ul></ul></ul><ul><ul><ul><li>Use its writeObject method </li></ul></ul></ul><ul><ul><li>Deserialization: Reading an Object Stream </li></ul></ul><ul><ul><ul><li>Use the ObjectInputStream class </li></ul></ul></ul><ul><ul><ul><li>Use its readObject method </li></ul></ul></ul>

×