IO In Java

  • Upload
    parag

  • View
    3.149

  • Download
    4

Embed Size (px)

Citation preview

PowerPoint Presentation

I/O In Java

Parag Shah

Adaptive Software Solutions

http://www.adaptivesoftware.bizhttp://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

Adaptive Software Solutions 2002 - 2007 http://www.adaptivesoftware.biz

Adaptive Software Solutions 2002 - 2007 http://www.adaptivesoftware.biz