0
I/O In Java Parag Shah Adaptive Software Solutions http://www.adaptivesoftware.biz http://www.adaptivelearningonline.net
Agenda <ul><li>Files </li></ul><ul><li>Streams </li></ul><ul><li>Serialization </li></ul><ul><li>Tokenization </li></ul>
Overview  <ul><li>What should an IO system encompass? </li></ul><ul><ul><li>Abstractions for the following </li></ul></ul>...
Vocabulary <ul><li>Streams </li></ul><ul><li>Buffers </li></ul><ul><li>Readers </li></ul><ul><li>Writers </li></ul>
Standard IO <ul><li>Reading from StandardInput (see Echo.java) </li></ul><ul><ul><ul><li>System.in (Raw IOStream) </li></u...
A Simple Example <ul><li>System.out.println(); </li></ul><ul><li>System is a class </li></ul><ul><li>out is a static attri...
A Simple Example contd. <ul><li>System.in.read(); </li></ul><ul><li>BufferedReader br = new BufferedInputStream(new InputS...
Files & Directories <ul><li>The File class is an abstract represention of file and directory pathnames </li></ul><ul><li>I...
Streams <ul><li>What are Streams? </li></ul><ul><ul><ul><li>Arbitrary data source/destination </li></ul></ul></ul><ul><ul>...
InputStream <ul><li>Various sources of data </li></ul><ul><ul><ul><li>Array of bytes </li></ul></ul></ul><ul><ul><ul><li>S...
InputStream <ul><li>See  [StreamExample.java] </li></ul>
OutputStream <ul><li>Data Sinks </li></ul><ul><ul><ul><li>ByteArray </li></ul></ul></ul><ul><ul><ul><li>File </li></ul></u...
OutputStream <ul><li>See [StreamExample.java] </li></ul>
FilterInputStream <ul><li>Used to format/filter data being read from an InputStream </li></ul><ul><li>Types of FilterInput...
FilterOutputStream <ul><li>Used to write formatted/filtered data to an OutputStream </li></ul><ul><li>Types of FilterOutpu...
Readers & Writers <ul><li>Purpose of Readers & Writers </li></ul><ul><ul><ul><li>Input/Output Stream are for byte based I/...
The Reader Hierarchy <ul><li>The Reader Hierarchy </li></ul>
The Writer Hierarchy <ul><li>The Writer Hierarchy </li></ul>
IO Best Practices <ul><li>Best Practice </li></ul><ul><ul><ul><li>Use Readers / Writers for character IO </li></ul></ul></...
Object Serialization - 1 <ul><li>What is serialization </li></ul><ul><li>When do we need serialization </li></ul>
Object Serialization - 2 <ul><li>Serializing objects </li></ul><ul><ul><li>ObjectOutputStream </li></ul></ul><ul><li>Deser...
Object Serialization - 3 <ul><li>Controlling serialization </li></ul><ul><ul><li>The Serializable interface </li></ul></ul...
Tokenizing <ul><li>What are tokens? </li></ul><ul><li>StringTokenizer </li></ul><ul><li>StreamTokenizer </li></ul>
String Tokenizer <ul><li>Tokenizes a String </li></ul><ul><li>We must define delimiting characters </li></ul><ul><li>Throw...
Stream Tokenizer <ul><li>Tokenizes an input stream </li></ul><ul><li>Parsing process is controlled by a table and flags </...
Stream Tokenizer <ul><li>The character value is used to look up five possible attributes of the character </li></ul><ul><u...
Stream Tokenizer <ul><li>Each tokenizer has four flags :- </li></ul><ul><ul><li>Whether line terminators are to be returne...
Using Stream Tokenizer <ul><li>Create an instance of StreamTokenizer </li></ul><ul><li>Set up tables and flags </li></ul><...
Summary <ul><li>Streams </li></ul><ul><li>Types of Input & Output streams </li></ul><ul><li>The decorator design pattern <...
Where to Get More Information <ul><li>Thinking In Java By Bruce Eckel </li></ul><ul><li>Sun Java Trail </li></ul><ul><ul><...
Upcoming SlideShare
Loading in...5
×

IO In Java

2,673

Published on

Published in: Technology, Education
2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,673
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
125
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "IO In Java"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×