40-244 – advanced programming p rogramming in lecture 22 input and output system

22
40-244 – Advanced Programming PROGRAMMING IN Lecture 22 Input and Output System

Upload: david-moore

Post on 18-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

40-244 – Advanced Programming

PROGRAMMING IN

Lecture 22

Input and Output System

Page 2: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

What We Will Learn

Using directory listings

Using Streams

Decorators

Readers and Writers

Tokenizers

Page 3: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Files and Directories

File class in java.io package

Represents name of a file or a set of files You cannot read or write data using File

Used for Manipulating filenames Working with directories Checking file attributes

Page 4: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

File Example

Using File.list method

Directory lister example from TIJ Chap. 11 DirList.java DirList3.java (using anonymous inner classes)

Page 5: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Input and Output

Looking into input and output in an abstract way

Input: Reading data from a source

Source: anything that we can read data items from it

Output: Writing data to a sink

Sink: anything that we can write data items to it

Page 6: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Streams

Abstract class representing a data source/sink

InputStream: Represents a source Provides read() methods

OutputStream: Represent a sink Provides write() methods

Page 7: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Kinds of Streams

Streams are categorized according to type of source or sink they represent Files Arrays of bytes Strings Pipes Internet connections

Page 8: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

InputStream Example

Using echo: echo(System.in) echo(new FileInputStream("test.txt"))

It works for any kind of input stream

public void echo(InputStream in)

throws IOException {

int b;

while ((b = in.read()) != -1)

System.out.print((char)b);

}

Page 9: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Input Stream Classes

ByteArrayInputStream Arrays of bytes

FileInputStream Files

StringBufferInputStream Strings

ObjectInputStream Serialized objects

Page 10: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Adding Features

Adding various features to streams such as: Compression Line-numbering Buffering Push-back

Combining these features with every stream causes inheritance tree explosion! CompressedFileInputStream BufferedFileInputStream CompressedBufferedFileInputStream, ...

Page 11: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Filter Streams

Wrap around other streams

Adding features to them

Without changing the interface

Such as: BufferedInputStream LineNumberInputStream PushbackInputStream DataInputStream ZipInputStream

Page 12: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Filter Example

We want to read from a file which is compressed with buffering mechanism with ability to pushback data into it

We use one filter for each

Page 13: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Filter Example

close

read

File I S close

read

Buffered I S close

read

Zip I S

Pushback I S

close

read

Page 14: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Using Filters

After construction, in can be used as an ordinary input stream

Additional methods for pushback is available

PushbackInputStream in = new PushbackInputStream(

new ZipInputStream(

new BufferedInputStream(

new FileInputStream("in.dat"))));

echo(in);

in.pushback(89);

in.close();

Page 15: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Decorator Pattern

All filters are subclasses of FilterInputStream

Which is a InputStream itself

Takes an InputStream upon construction

And filters data going through it

The interface is not changed

Since the filter is also an Input Stream

This way to define classes to add features is called the Decorator pattern

Page 16: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Another Example

A class for downloading URLs

Downloader.java

Page 17: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Readers and Writers

A separate hierarchy but pretty similar to streams

Added from Java 1.1 to handle Unicode IO Reader is similar to InputStream Writer is similar to OutputStream Similar usage of filters

Page 18: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Readers Example

Using echoLn: echoLn(new FileReader("in.txt"));

public void echoLn(Reader in) throws IOException {

LineNumberReader st = new LineNumberReader(in);

String line;

while ((line = st.readLine()) != null)

System.out.println(st.getLineNumber() + ":" +

line);

}

Page 19: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Streams to Readers

To read from a stream as a reader, use InputStreamReader

OutputStreamWriter is used for output

Reverse conversion is invalid

Reader r = new InputStreamReader(System.in);

echoLn(r);

Page 20: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Formatted Input

Is not easy!

One way is to read lines and then parse items

Using tokenizers is another way StringTokenizer: breaking strings into tokens StreamTokenizer: reads from a stream or

reader token by token

Page 21: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

StreamTokenizer

Can be used as a scanner

Set of configurable items: whitespaces, punctuations, comment characters

Can recognize ordinary tokens numbers quoted strings comments

Page 22: 40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

Tokenizer Examples

WordCount.java from TIJ Chapter 11 is a good example

ComicsManager.java also uses StreamTokenizer to read the configuration file Note about comments