Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

IO and serialization

1,716 views

Published on

Published in: Business, Technology
  • Be the first to comment

  • Be the first to like this

IO and serialization

  1. 1. IADCS Diploma Course Input/Output and Serialization U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  2. 2. How the Java Platform Supports I/O <ul><li>The package java.io supports console I/O and file I/O </li></ul><ul><li>Console I/O is character keyboard input and output without the mouse graphics of a graphical user interface </li></ul><ul><li>File I/O involves reading and writing data to and from a mass storage device, typically the computer’s hard drive </li></ul><ul><li>The new I/O (NIO) package java.nio supplements the original I/O package </li></ul>
  3. 3. Programming I/O <ul><li>In stream I/O, characters or bytes are read or written sequentially </li></ul><ul><li>The Java platform includes two dual hierarchies of classes that support streams: </li></ul><ul><ul><li>byte-oriented input and output </li></ul></ul><ul><ul><li>character-oriented input and output </li></ul></ul><ul><li>The class RandomAccessFile is used to read and write arbitrary locations within a file without first having to read or write all the bytes or characters that precede that location </li></ul>
  4. 4. Byte-Oriented Stream Classes
  5. 5. Predefined Stream Objects <ul><li>All Java programs can use three stream objects that are defined in the System class of the java.lang package: </li></ul><ul><ul><li>System.in </li></ul></ul><ul><ul><ul><li>The field System.in is a java.io.BufferedInputStream object </li></ul></ul></ul><ul><ul><li>System.out </li></ul></ul><ul><ul><ul><li>The field System.out is a java.io.PrintStream object </li></ul></ul></ul><ul><ul><li>System.err </li></ul></ul><ul><ul><ul><li>The field System.err is a java.io.PrintStream object </li></ul></ul></ul>
  6. 6. Input Methods <ul><li>Input methods of the java.io.InputStream class: </li></ul><ul><ul><li>int available() </li></ul></ul><ul><ul><li>void close() </li></ul></ul><ul><ul><li>void mark( int readlimit ) </li></ul></ul><ul><ul><li>boolean markSupported() </li></ul></ul><ul><ul><li>int read() </li></ul></ul><ul><ul><li>int read(byte[] buffer ) </li></ul></ul><ul><ul><li>int read( byte[] buffer , int offset , int length ) </li></ul></ul><ul><ul><li>void reset() </li></ul></ul><ul><ul><li>long skip(long bytecount ) </li></ul></ul>
  7. 7. Output Methods <ul><li>Output methods of the java.io.OutputStream class: </li></ul><ul><ul><li>void close() </li></ul></ul><ul><ul><li>void flush() </li></ul></ul><ul><ul><li>void write( int b ) </li></ul></ul><ul><ul><li>void write(byte[] buffer ) </li></ul></ul><ul><ul><li>void write( byte[] buffer , int offset , int length ) </li></ul></ul>
  8. 8. Other Byte I/O Classes <ul><li>Extensions of InputStream include the following classes: </li></ul><ul><ul><li>The class ByteArrayInputStream lets you read an array of bytes as though it were an InputStream object </li></ul></ul><ul><ul><li>The class SequenceInputStream provides a mechanism for concatenating the data from two or more InputStream objects into a single, seamless stream </li></ul></ul><ul><ul><li>The class PipedInputStream implements half of a pipe to connect the input stream of one thread or process to the output stream of another thread process </li></ul></ul>
  9. 9. Other Byte I/O Classes <ul><li>Extensions of OutputStream include the following classes : </li></ul><ul><ul><li>The class ByteArrayOutputStream sends its output into an object of type byte </li></ul></ul><ul><ul><li>The class PipedOutputStream is the complementary class to PipedInputStream </li></ul></ul>
  10. 10. Parsing an Input Stream <ul><li>The next step after reading a line of input is to parse it, or break it into separate tokens </li></ul><ul><li>A token usually consists of a sequence of characters that does not include a space, newline, tab, or other nonprinting character </li></ul><ul><li>To tokenize a string, you can use the StringTokenizer class provided by the java.util package </li></ul>
  11. 11. Random-Access File I/O <ul><li>The class RandomAccessFile supports byte-oriented I/O to and from random-access files </li></ul><ul><li>The class RandomAccessFile extends Object </li></ul><ul><li>RandomAccessFile combines input and output operations in one class </li></ul><ul><li>The seek method selects the position within the file where the next I/O operation will begin </li></ul>
  12. 12. Character Streams
  13. 13. Connecting Byte & Character I/O Classes <ul><li>The Java platform includes adapter classes that bridge between character I/O classes and byte I/O classes : </li></ul><ul><ul><li>InputStreamReader </li></ul></ul><ul><ul><li>OutputStreamWriter </li></ul></ul>
  14. 14. Using Other Character I/O Classes <ul><li>Extensions of the Reader object include the following: </li></ul><ul><ul><li>The class CharArrayReader lets you read an array of characters as though it were a Reader object </li></ul></ul><ul><ul><li>The class StringReader lets you read a String object as though it were a Reader object </li></ul></ul><ul><ul><li>The class PipedReader implements half of a pipe and is especially useful for communication between threads. </li></ul></ul>
  15. 15. Using Other Character I/O Classes <ul><li>Extensions of the Writer object include the following: </li></ul><ul><ul><li>The class CharArrayWriter sends its output into an object of type char[] </li></ul></ul><ul><ul><li>The class StringWriter lets you write to a StringBuffer object as though it were a Writer object </li></ul></ul><ul><ul><li>The class PipedWriter is the complementary class to PipedReader </li></ul></ul><ul><ul><li>The class PrintWriter is the character I/O equivalent of the PrintStream class </li></ul></ul>
  16. 16. The New I/O (NIO) Interface <ul><li>The java.nio package provides new features : </li></ul><ul><ul><li>Support for buffer management </li></ul></ul><ul><ul><li>A new primitive I/O abstraction called a channel </li></ul></ul><ul><ul><li>File locking at the process level </li></ul></ul><ul><ul><li>Memory mapping </li></ul></ul>
  17. 17. Buffers <ul><li>Capacity: the maximum number of data elements that the buffer can contain </li></ul><ul><li>Limit : a reflection of the amount of data that the buffer currently contains and is defined as the index of the first element in the buffer that should not be read or written </li></ul><ul><li>Mark : the index to which the position value will be set if the buffer is reset  </li></ul><ul><li>Position : the index of the next element to be read or written and can never exceed the limit </li></ul>
  18. 18. Buffer Classes in java.nio
  19. 19. Object Serialization <ul><li>Object serialization : a general solution that lets you write objects to I/O streams and then read them, without defining any additional methods </li></ul><ul><li>Enables t ransmission of objects over a network or save objects to files between runs of your program </li></ul><ul><li>The object serialization feature is an important component of the Java Remote Method Invocation (RMI) enterprise API </li></ul>
  20. 20. Using Object Streams <ul><li>Two stream classes support object serialization: </li></ul><ul><ul><li>ObjectOutputStream - a subclass of OutputStream </li></ul></ul><ul><ul><li>ObjectInputStream - a subclass of InputStream </li></ul></ul><ul><li>To create an ObjectOutputStream object, provide an existing OutputStream object as the argument of the constructor </li></ul><ul><li>To create an ObjectInputStream object, provide an existing InputStream object as the argument of the constructor </li></ul>
  21. 21. Suppressing Serialization of Fields <ul><li>Include the qualifier transient when you declare fields to indicate that they should not be serialized with instances of the class </li></ul><ul><li>Fields that have the transient qualifier are not output when the object is serialized </li></ul><ul><li>When the object is deserialized later, transient fields are given the default value normally used for fields of their type </li></ul>

×