KEMBAR78
IO In Java | ODP
I/O In Java Parag Shah Adaptive Software Solutions http://www.adaptivesoftware.biz http://www.adaptivelearningonline.net
Agenda Files Streams Serialization Tokenization
Overview  What should an IO system encompass? Abstractions for the following Communicating with various sources and sinks of I/O Console Files Network sockets... Doing the I/O in multiple ways Character Binary Buffered... Approach taken by Java for the IO system
Vocabulary Streams Buffers Readers Writers
Standard IO Reading from StandardInput (see Echo.java) System.in (Raw IOStream) Writing to Standard Output System.out (PrintStream) Standard Error System.err (PrintStream) Redirecting standard IO  [see: Redirecting.java]
A Simple Example System.out.println(); System is a class out is a static attribute of system out is of type PrintStream
A Simple Example contd. System.in.read(); BufferedReader br = new BufferedInputStream(new InputStreamReader(System.in)); System is a class in is a static attribute of system in is of type InputStream InputStreamReader converts an InputStream to the Reader hierarchy BufferedReader adds buffering and line reading capabilities to the Reader
Files & Directories The File class is an abstract represention of file and directory pathnames It does NOT represent a file see  FileExample.java RandomAccessFile  does represent a file Can read or write at arbitrary locations
Streams What are Streams? Arbitrary data source/destination Details of the device is abstracted InputStream Data input source Abstract  class java.io.InputStream OutputStream Data output destination Abstract  class java.io.OutputStream
InputStream Various sources of data Array of bytes String object File Pipe
InputStream See  [StreamExample.java]
OutputStream Data Sinks ByteArray File Pipe
OutputStream See [StreamExample.java]
FilterInputStream Used to format/filter data being read from an InputStream Types of FilterInputStream DataInputStream BufferedInputStream LineNumberInputStream PushbackInputStream
FilterOutputStream Used to write formatted/filtered data to an OutputStream Types of FilterOutputStream DataOutputStream PrintStream BufferedOutputStream
Readers & Writers Purpose of Readers & Writers Input/Output Stream are for byte based I/O Readers for unicode based char I/O Modifying stream behaviour FilterReader FilterWriter (abstract class with no subclasses)
The Reader Hierarchy The Reader Hierarchy
The Writer Hierarchy The Writer Hierarchy
IO Best Practices Best Practice Use Readers / Writers for character IO Use Input/OutputStream for byte based IO For reading/writing data in a portable manner use DataInput(Output)Stream Use buffering Always close the streams
Object Serialization - 1 What is serialization When do we need serialization
Object Serialization - 2 Serializing objects ObjectOutputStream Deserializing objects ObjectInputStream Remember no constructors are called see  [SerializationExample.java]
Object Serialization - 3 Controlling serialization The Serializable interface The  transient  keyword The Externalizable interface
Tokenizing What are tokens? StringTokenizer StreamTokenizer
String Tokenizer Tokenizes a String We must define delimiting characters Throws an Exception if we go beyond the String size. See  [StringTokenizingExample.java] It is recommended to use the split() method in String See  [StringSplitExample.java]
Stream Tokenizer Tokenizes an input stream Parsing process is controlled by a table and flags Each byte read from the input stream is regarded as a character in the range '\u0000' through '\u00FF'
Stream Tokenizer The character value is used to look up five possible attributes of the character White space Alphabetic Numeric String quote Comment character Each character can have zero or more of these attributes
Stream Tokenizer Each tokenizer has four flags :- Whether line terminators are to be returned as tokens or treated as white space that merely separates tokens. Whether C-style comments are to be recognized and skipped. Whether C++-style comments are to be recognized and skipped. Whether the characters of identifiers are converted to lowercase.
Using Stream Tokenizer Create an instance of StreamTokenizer Set up tables and flags Loop through the stream calling nextToken until a TT_EOF is returned. See  [StreamTokenizingExample.java]
Summary Streams Types of Input & Output streams The decorator design pattern The need for so many stream classes Serialization Tokenizing
Where to Get More Information Thinking In Java By Bruce Eckel Sun Java Trail http://java.sun.com/docs/books/tutorial/essential/io/index.html

IO In Java

  • 1.
    I/O In JavaParag Shah Adaptive Software Solutions http://www.adaptivesoftware.biz http://www.adaptivelearningonline.net
  • 2.
    Agenda Files StreamsSerialization Tokenization
  • 3.
    Overview Whatshould an IO system encompass? Abstractions for the following Communicating with various sources and sinks of I/O Console Files Network sockets... Doing the I/O in multiple ways Character Binary Buffered... Approach taken by Java for the IO system
  • 4.
  • 5.
    Standard IO Readingfrom StandardInput (see Echo.java) System.in (Raw IOStream) Writing to Standard Output System.out (PrintStream) Standard Error System.err (PrintStream) Redirecting standard IO [see: Redirecting.java]
  • 6.
    A Simple ExampleSystem.out.println(); System is a class out is a static attribute of system out is of type PrintStream
  • 7.
    A Simple Examplecontd. System.in.read(); BufferedReader br = new BufferedInputStream(new InputStreamReader(System.in)); System is a class in is a static attribute of system in is of type InputStream InputStreamReader converts an InputStream to the Reader hierarchy BufferedReader adds buffering and line reading capabilities to the Reader
  • 8.
    Files & DirectoriesThe File class is an abstract represention of file and directory pathnames It does NOT represent a file see FileExample.java RandomAccessFile does represent a file Can read or write at arbitrary locations
  • 9.
    Streams What areStreams? Arbitrary data source/destination Details of the device is abstracted InputStream Data input source Abstract class java.io.InputStream OutputStream Data output destination Abstract class java.io.OutputStream
  • 10.
    InputStream Various sourcesof data Array of bytes String object File Pipe
  • 11.
    InputStream See [StreamExample.java]
  • 12.
    OutputStream Data SinksByteArray File Pipe
  • 13.
  • 14.
    FilterInputStream Used toformat/filter data being read from an InputStream Types of FilterInputStream DataInputStream BufferedInputStream LineNumberInputStream PushbackInputStream
  • 15.
    FilterOutputStream Used towrite formatted/filtered data to an OutputStream Types of FilterOutputStream DataOutputStream PrintStream BufferedOutputStream
  • 16.
    Readers & WritersPurpose of Readers & Writers Input/Output Stream are for byte based I/O Readers for unicode based char I/O Modifying stream behaviour FilterReader FilterWriter (abstract class with no subclasses)
  • 17.
    The Reader HierarchyThe Reader Hierarchy
  • 18.
    The Writer HierarchyThe Writer Hierarchy
  • 19.
    IO Best PracticesBest Practice Use Readers / Writers for character IO Use Input/OutputStream for byte based IO For reading/writing data in a portable manner use DataInput(Output)Stream Use buffering Always close the streams
  • 20.
    Object Serialization -1 What is serialization When do we need serialization
  • 21.
    Object Serialization -2 Serializing objects ObjectOutputStream Deserializing objects ObjectInputStream Remember no constructors are called see [SerializationExample.java]
  • 22.
    Object Serialization -3 Controlling serialization The Serializable interface The transient keyword The Externalizable interface
  • 23.
    Tokenizing What aretokens? StringTokenizer StreamTokenizer
  • 24.
    String Tokenizer Tokenizesa String We must define delimiting characters Throws an Exception if we go beyond the String size. See [StringTokenizingExample.java] It is recommended to use the split() method in String See [StringSplitExample.java]
  • 25.
    Stream Tokenizer Tokenizesan input stream Parsing process is controlled by a table and flags Each byte read from the input stream is regarded as a character in the range '\u0000' through '\u00FF'
  • 26.
    Stream Tokenizer Thecharacter value is used to look up five possible attributes of the character White space Alphabetic Numeric String quote Comment character Each character can have zero or more of these attributes
  • 27.
    Stream Tokenizer Eachtokenizer has four flags :- Whether line terminators are to be returned as tokens or treated as white space that merely separates tokens. Whether C-style comments are to be recognized and skipped. Whether C++-style comments are to be recognized and skipped. Whether the characters of identifiers are converted to lowercase.
  • 28.
    Using Stream TokenizerCreate an instance of StreamTokenizer Set up tables and flags Loop through the stream calling nextToken until a TT_EOF is returned. See [StreamTokenizingExample.java]
  • 29.
    Summary Streams Typesof Input & Output streams The decorator design pattern The need for so many stream classes Serialization Tokenizing
  • 30.
    Where to GetMore Information Thinking In Java By Bruce Eckel Sun Java Trail http://java.sun.com/docs/books/tutorial/essential/io/index.html