lecture 6. java i/o

84
Java Programming Transparency No. 1 Lecture 6. Java I/O Cheng-Chia Chen

Upload: gautam

Post on 11-Jan-2016

62 views

Category:

Documents


1 download

DESCRIPTION

Lecture 6. Java I/O. Cheng-Chia Chen. Contents. Overview of I/O Streams Using the Data Sink Streams How to Use File Streams How to Use Pipe Streams Using the Processing Streams How to Concatenate Files Working with Filtered Streams How to Use DataInputStream and DataOutputStream - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 6.  Java I/O

Java Programming

Transparency No. 1

Lecture 6. Java I/O

Cheng-Chia Chen

Page 2: Lecture 6.  Java I/O

Java I/O

Transparency No. 2

Contents

1. Overview of I/O Streams

2. Using the Data Sink Streams 1. How to Use File Streams

2. How to Use Pipe Streams

3. Using the Processing Streams 1. How to Concatenate Files

2. Working with Filtered Streams 1. How to Use DataInputStream and DataOutputStream

2. Writing Your Own Filtered Streams

3. Object Serialization 1. Serializing Objects

2. Providing Object Serialization for Your Classes

4. Working with Random Access Files 1. Using Random Access Files

2. Writing Filters for Random Access Files

Page 3: Lecture 6.  Java I/O

Java I/O

Transparency No. 3

I/O Streams

A stream is a sequence of bytes (or data or objects) that flow from a source to a destination

In a program, we read information from an input stream and write information to an output stream

for output

for input

Page 4: Lecture 6.  Java I/O

Java I/O

Transparency No. 4

procedure for reading/writing data to/from an input/output stream

general program schema for for reading and writing data from/to an Input/output stream: // for reading: 1. open an input stream // open 2. while there is more information // reading read next data from the stream 3. close the stream. // close // for writing 1. open an output stream // open 2. while there is more information // writing write data to the stream 3. close the stream. // close

Page 5: Lecture 6.  Java I/O

Java I/O

Transparency No. 5

I/O Stream Categories

The java.io package contains many classes that allow us to define various streams with specific characteristics

The classes in the I/O package divide input and output streams into other categories

An I/O stream is either a character stream, which deals with text data byte stream, which deal with byte data

An I/O stream is also either a data stream, which acts as either a source or destination processing stream, which alters or manages information in the stream

Page 6: Lecture 6.  Java I/O

Java I/O

Transparency No. 6

classification of Java I/O streams and their naming conventions By I/O Directions:

for Input => Input for output => Output

By datatype of stream content char data => Reader / Writer byte data => InputStream / OutputStream other primitive data(int, long, float,…) => DataInputStream /DataOutputStream Objects => ObjectInputStream /ObjectOutputStream

By characteristic of stream source / destination 1. for final device : data sink stream file, byte/char array, StringBuffer, pipe purpose: serve as the source/destination of the stream. 2. for intermediate process : processing streams Buffering, Filtering, Byte2Char, Char2Byte, etc. purpose: alters or manages information in the stream

Page 7: Lecture 6.  Java I/O

Java I/O

Transparency No. 7

The java.io package

InputStream

OutputStream

Reader

Writer

Page 8: Lecture 6.  Java I/O

Java I/O

Transparency No. 8

java.io.InputStream and its subclasses

gray => data sink stream white => processing stream // require other streams

Page 9: Lecture 6.  Java I/O

Java I/O

Transparency No. 9

The Java.io.OutputStream and its subclasses

Page 10: Lecture 6.  Java I/O

Java I/O

Transparency No. 10

java.io.Reader and its subclasses

Page 11: Lecture 6.  Java I/O

Java I/O

Transparency No. 11

java.io.Writer and its subclasses

Page 12: Lecture 6.  Java I/O

Java I/O

Transparency No. 12

Basic methods provided in java input classes

Reader and InputStream define similar APIs but for different data types.

Reader contains methods for reading characters and arrays of characters: int read() int read(char cbuf[]) int read(char cbuf[], int offset, int length)

InputStream defines the same methods but for reading bytes and arrays of bytes: int read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length)

Both classes provide methods for marking a location in the stream, skipping input, and resetting the current position.

Page 13: Lecture 6.  Java I/O

Java I/O

Transparency No. 13

Basic methods provided in java output streams

Writer defines these methods for writing character, string and arrays of characters void write(int c) // 16 low-ordered bits of c is written void write(char[] cbuf [, int off, int len ] ) void write(String s [, int off, int len ] )

OutputStream defines the same methods but for bytes: void write(int c) // 8 low-ordered bits of c is written void write(byte cbuf[]) void write(byte cbuf[], int offset, int length)

All of the streams--readers, writers, input streams, and output streams— are automatically opened when created ! can be closed by calling its close() method, or the garbage collector. will throw IOException on failure of IO operations.

Page 14: Lecture 6.  Java I/O

Java I/O

Transparency No. 14

Standard I/O

There are three standard I/O streams: standard input – defined by System.in standard output – defined by System.out standard error – defined by System.err

We use System.out when we execute println statements System.out and System.err are of the type PrinterStream.

System.in is declared to be a generic InputStream reference, and therefore usually must be mapped to a more useful stream with specific characteristics eg: to read char instead of bytes => InputStreamReader cin = new InputStreamReader(System.in);

Page 15: Lecture 6.  Java I/O

Java I/O

Transparency No. 15

java.io.InputStream public abstract class InputStream extends Object Constructor : InputStream() // default no-arg constuctorMethod Summary int available()

Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next read(…) .

void close() Closes this input stream and releases any system resources associated with the stre

am. abstract int read( [ byte[] b [, int offset, int length ]] )

Reads the next byte of data from the input stream. Reads some number of bytes from the input stream and stores them into the buffer a

rray b. long skip(long n)

Skips over and discards n bytes of data from this input stream.

Page 16: Lecture 6.  Java I/O

Java I/O

Transparency No. 16

Methods for marking operaqtions

void mark(int readlimit) Marks the current position in this input stream. readlimit is the number of bytes that can be read without invalidating th

is mark operation.

void reset() Repositions this stream to the position at the time the mark method wa

s last called on this input stream.

boolean markSupported() Tests if this input stream supports the mark and reset methods.

Page 17: Lecture 6.  Java I/O

Java I/O

Transparency No. 17

java.io.Reader public abstract class Reader extends Object Abstract class for reading character streams.

The only methods that a subclass must implement are read(char[], int, int) and close().

Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Field Summary protected Object lock

The object used to synchronize operations on this stream. use lock instead of this or synchronized method for efficient time-critical operations

Constructor Summary protected Reader()

Create a new character-stream reader whose critical sections will synchronize on the reader itself.

protected Reader(Object lock) Create a new character-stream reader whose critical sections will synchronize on the

given object.

Page 18: Lecture 6.  Java I/O

Java I/O

Transparency No. 18

Methods summary of java.io.Reader

abstract void close() // Close the stream. int read() // Read a single character. int read(char[] cbuf [,int off, int len] )

Read characters into an array.

boolean ready() // replacement of available() in java.InputStream Tell whether this stream is ready to be read [ without causing I/Oblocking in next read()].

long skip(long n) // Skip characters. void mark(int limit)

Mark the present position in the stream. limit is the number of chars that can be read without causing failure of the following reset().

void reset() // Reset the stream. boolean markSupported()

Tell whether this stream supports the mark() operation.

Page 19: Lecture 6.  Java I/O

Java I/O

Transparency No. 19

Java.io.OutputStream

public abstract class OutputStream extends Object the superclass of all classes representing an output stream of bytes.

An output stream accepts output bytes and sends them to some sink. Subclasses must always provide at least a method that writes one byte of output.

Constructor Summary OutputStream() // will automatically open this stream for writing

Methods summary void close() // Closes this output stream and releases system resources. void flush()

Flushes this output stream and forces any buffered output bytes to be written out.

void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this output strea

m.

abstract void write( int b) // Writes the specified byte to this output stream.

Page 20: Lecture 6.  Java I/O

Java I/O

Transparency No. 20

java.io.Writer

public abstract class Writer extends Object Abstract class for writing to character streams.

Subclass must implement write(char[], int, int), flush(), and close(). subclasses may override some of the methods defined here in order to provide highe

r efficiency, additional functionality, or both.

Field Summary protected Object lock

The object used to synchronize operations on this stream.

Constructor Summary protected Writer()

Create a new character-stream writer whose critical sections will synchronize on the writer itself.

protected Writer(Object lock) Create a new character-stream writer whose critical sections will synchronize on the

given object.

Page 21: Lecture 6.  Java I/O

Java I/O

Transparency No. 21

java.io.Writer method summary

abstract void close() // Close the stream, flushing it first. abstract void flush() // Flush the stream. abstract void write(char[] cbuf, int off, int len)

Write a portion of an array of characters.

void write(int c) // Write a single character. void write(String str) // Write a string. void write(String str, int off, int len)

Write a portion of a string.

Page 22: Lecture 6.  Java I/O

Java I/O

Transparency No. 22

JDK1.3 ‘s implementation of write( int )

protected Object lock; // for synchronization

private char[] writeBuffer; // for chars to be output

private final int writeBufferSize = 1024;

public void write( int c) throws IOException {

synchronized (lock) {

if (writeBuffer == null){

writeBuffer = new char[writeBufferSize];

}

writeBuffer[0] = (char) c;

write(writeBuffer, 0, 1);

} } Subclasses that intend to support efficient single-character output

should override this method.

Page 23: Lecture 6.  Java I/O

Java I/O

Transparency No. 23

2. The Data Sink Streams

Types of data sinks: memory[array, strings], files, or pipes Naming convention: <sinkType><I/O;CharOrByte>

Sink type character stream byte stream

arrayCharArrayReader, CharArrayWriter

ByteArrayInputStream, ByteArrayOutputStream

StringStringReader, StringWriter

StringBufferInputStream

(deprecated)

PipePipedReader, PipedWriter

PipedInputStream, PipedOutputStream

FileFileReader, FileWriter

FileInputStream, FileOutputStream

Page 24: Lecture 6.  Java I/O

Java I/O

Transparency No. 24

Summary of the data sink streams

FileReader, FileWriter; FileInputStream, FileOutputStream Collectively called file streams; used to read from or write to a file on th

e native file system.

CharArrayReader, CharArrayWriter; ByteArrayInputStream, ByteArrayOutputStream Use these streams to read from and write to memory array. create these streams on an existing array and then use the read and wri

te methods to read from or write to the array. Constructors: <Type> = new <Type>( {char | byte } [] [, int offset, int le

ngth ] ) Ex: CharArrayWriter cwr = new CharArrayWriter( new char[100] ) ; ByteArrayInputstream bi = new ByteArrayInputStream ( new byte[] {\011,\022, \0xff} );

Page 25: Lecture 6.  Java I/O

Java I/O

Transparency No. 25

String Streams

StringReader, StringWriter, StringBufferInputStream StringReader : used to read characters from a String. StringWriter : used to to write to a String. StringWriter collects the char

acters written to it in a implicit StringBuffer, which can then be converted to a String.

StringBufferInputStream : similar to StringReader, except that it reads bytes from a StringBuffer. (deprecated since it cannot correctly convert a char to bytes; use StringReader instead)

Constructors: StringReader(String); StringWriter( [int initSize] ) // initial buffer size use toString() to get th

e written String StringBufferInputStrean(String) // deprecfated ! Only the low eight bit

s of each character in the string are used by this class.

Page 26: Lecture 6.  Java I/O

Java I/O

Transparency No. 26

Piped Streams

PipedReader, PipedWriter; PipedInputStream, PipedOutputStream

Implement the input and output components of a pipe. Pipes are used to channel the output from one program (or thread) into

the input of another. PipedReader and PipedInputStream get data from the output of another

thread, while PipedWriter and PipedOutputStream output data for processing by other thread.

Page 27: Lecture 6.  Java I/O

Java I/O

Transparency No. 27

2.1 How to Use File Streams

File streams are the easiest streams to understand. Create (and open )the file stream:

<FileStream> f = new <FileStream>( <file> ); where <FileStream> is any of FileReader, FileWriter, FileInputStream, an

d FileOutputStream and <file> is either a string for file name, a File object, or a FileDescriptor obj

ect (low-level handle to open file or socket).

EX: FileReader reader1 = new FileReader(“example1.txt”); File file1 = new File(“d:\\java\\examples\\ex1.java”); FilerWriter writer1 = new FileWriter(file1); FileInputStream in1 = new FileInputStream(“ex2”); FileOutputStream out1 = new FileOutputStream ( java.io.FileDescriptor.o

ut );

Page 28: Lecture 6.  Java I/O

Java I/O

Transparency No. 28

A simple file-copy program Copy the contents of a file named input.txt into a file called output.txt: import java.io.*; public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File(“input.txt"); File outputFile = new File("output.txt");

FileReader in = new FileReader( inputFile ); FileWriter out = new FileWriter( outputFile );

int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } }

Page 29: Lecture 6.  Java I/O

Java I/O

Transparency No. 29

Copy bytes

import java.io.*;public class CopyBytes { public static void main(String[] args) throws IOException { File inputFile = new File(“input.txt"); File outputFile = new File("output.txt");

FileInputStream in = new FileInputStream(inputFile); FileOutputStream out = new FileOutputStream(outputFile);

int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); }}

Page 30: Lecture 6.  Java I/O

Java I/O

Transparency No. 30

2.2 How to Use Pipe Streams

A pipe is a channel for receiving data from one program (or thread) and sending the received data to the input of another.

PipedReader, PipedWriter ; PipedInputStream, PipedOutputStream implement the input and output components of a pipe.

for p2, p is an PipedWriter ( or PipedOutputStream ) for p1, p is a PipedReader ( or PipedInputstream )

p : a pipe input of program p1program p2: output

Page 31: Lecture 6.  Java I/O

Java I/O

Transparency No. 31

Pipe example

a simple program to reverse, sort and reverse words in a file. Note: without pipe, programmers would need 2 additional

temporary files/buffers to store the intermediate results.

pipe 1 pipe 2

sink(words)

sink(rhymedWords)

Page 32: Lecture 6.  Java I/O

Java I/O

Transparency No. 32

The program

import java.io.*;public class RhymingWords { public static void main(String[] args) throws IOException { FileReader words = new FileReader("words.txt");

// do the reversing and sorting Reader rhymedWords = reverse(sort(reverse(words)));

// write new list to standard out BufferedReader in = new BufferedReader( rhymedWords ); String input; while ((input = in.readLine()) != null) System.out.println(input); in.close(); }

Page 33: Lecture 6.  Java I/O

Java I/O

Transparency No. 33

the reverse method

public static Reader reverse(Reader source) throws IOException {

BufferedReader in = new BufferedReader(source);

PipedWriter pipeOut = new PipedWriter();

PipedReader pipeIn = new PipedReader( pipeOut );

// PipedReader pipeIn = new PipedReader( ); // alternative codes for

// PipedWriter pipeOut = new PipedWriter( pipeIn ); // the above two lines

PrintWriter out = new PrintWriter(pipeOut); // wrap pipeOut for easy write

new ReverseThread(out, in).start(); return pipeIn; }

reverse Thread

in(source)

out(pipeOut)

pipeOut

pipeIn

Page 34: Lecture 6.  Java I/O

Java I/O

Transparency No. 34

// source is a PipedReaderpublic static Reader sort(Reader source) throws IOException { BufferedReader in = new BufferedReader(source);

PipedWriter pipeOut = new PipedWriter(); PipedReader pipeIn = new PipedReader(pipeOut); PrintWriter out = new PrintWriter(pipeOut);

new SortThread(out, in).start(); return pipeIn; }}

Page 35: Lecture 6.  Java I/O

Java I/O

Transparency No. 35

ReverseThread.java

import java.io.*;public class ReverseThread extends Thread { private PrintWriter out; private BufferedReader in; public ReverseThread(PrintWriter out, BufferedReader in) { this.out = out; this.in = in; } public void run() { // entry point of a thread (invoked by start()), // like main() of a java program if (out != null && in != null) { try { String input; while ((input = in.readLine()) != null) { out.println(reverseIt(input)); out.flush(); } out.close(); } catch (IOException e) { System.err.println("ReverseThread run: " + e); } } } private String reverseIt(String source) { …} }

Page 36: Lecture 6.  Java I/O

Java I/O

Transparency No. 36

SortThread.javaimport java.io.*;public class SortThread extends Thread { private PrintWriter out = null; private BufferedReader in = null; public SortThread( PrintWriter out, BufferedReader in ) { this.out = out; this.in = in; } public void run() { int MAXWORDS = 50; if (out != null && in != null) { try { String[] listOfWords = new String[MAXWORDS]; int numwords = 0; // read words from in into listOfWords while ((listOfWords[numwords] = in.readLine()) != null) numwords++; quicksort(listOfWords, 0, numwords-1); // sort for (int i = 0; i < numwords; i++) out.println(listOfWords[i]); // output out.close(); } catch (IOException e) { System.err.println("SortThread run: " + e); } } }

Page 37: Lecture 6.  Java I/O

Java I/O

Transparency No. 37

quicksort

private static void quicksort(String[] a, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) return; String mid = a[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && a[lo].compareTo(mid) < 0) lo++; while (lo<hi && a[hi].compareTo(mid) > 0) hi--; if (lo < hi) { String T = a[lo]; a[lo] = a[hi]; a[hi] = T; lo++; hi--; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } quicksort(a, lo0, lo); quicksort(a, lo == lo0 ? lo+1 : lo, hi0); }}

Page 38: Lecture 6.  Java I/O

Java I/O

Transparency No. 38

3. Using the processing streams

Using Streams to Wrap Other Streams : The reverse method contains some other interesting code; in pa

rticular, these two statements: BufferedReader in = new BufferedReader(source); ... PrintWriter out = new PrintWriter(pipeOut);general format: WrappedClass w = new WrappedClass( rawObject )

where rawObjwect is an obejct of type rawClass which does not provide the needed method, while Wrapped class is an extension of rawClass with intended methods provided.

In revserse() method: source is a [file | pipe] Reader, which does not provide the readln() met

hod. pipeOut is a [piped] writer, which does not provide the convenient print

ln() method.

Page 39: Lecture 6.  Java I/O

Java I/O

Transparency No. 39

3.1 Types of the Processing Streams

process character stream byte stream

BufferingBufferedReader, BufferedWriter

BufferedInputStream, BufferedOutputStream

FilteringFilterReader, FilterWriter

FilterInputStream, FilterOutputStream

B2C and C2BInputStreamReader, OutputStreamWriter

StringBufferInputStream

(deprecated)

Concatenation SequenceInputStream

Object SerializationObjectInputStream, ObjectOutputStream

Data ConversionDataInputStream, DataOutputStream

Counting LineNumberReader LineNumberInputStream

printing PrintWriter PrintStream

Peeking Ahead PushbackReader PushbackInputStream

Page 40: Lecture 6.  Java I/O

Java I/O

Transparency No. 40

3.2 Summary of the processing streams

Buffered Streams Buffer data while reading or writing, thereby reducing the number of ac

cesses required on the original data source. typically more efficient than similar nonbuffered streams; suggest alw

ays wrap a buffer stream around any stream such as a file stream whose read/write operations are costly.

BufferedReader f = new BufferedReader(new FileReader(“data.txt”)); BufferedOutputStream b = new BufferedOutputStream ( new FileOutputStream(“dataout”));

raw Input Stream program raw Output Stream

buffer bufferBufferedInputStream BufferedOutputStream

Page 41: Lecture 6.  Java I/O

Java I/O

Transparency No. 41

Filter Streams

Filter streams contains some other input stream as its basic source of data, possibly t

ransforming the data along the way or providing additional functionality.

Abstract classes, same their parent streams (without new methods). They define the interface for filter streams, which filter data as it's bein

g read or written.

underlying Input Stream program underlying Output Stream

FilterInputStream

Transformation +additionalfunctionality

FilterOutputStream

transformation +additional functionality

Page 42: Lecture 6.  Java I/O

Java I/O

Transparency No. 42

Byte2Char and char2Byte

A reader and writer pair that forms the bridge between byte streams and character streams.

default encoding from system property : “file.encoding” Ex:

InputStreamReader reader = new InputStreamReader( new FileInputStream(“data.txt”) , “Big5” ); OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream(“out.txt”));

InputStream program OutputStream

InputStreamReaderencoding encoding

OutputStreamWriter

Page 43: Lecture 6.  Java I/O

Java I/O

Transparency No. 43

SequenceInputStream Concatenates multiple input streams into one input stream. ex: SequenceInputStream sin = new SequenceInputStream(s1,s2);

ObjectInputStream and ObjectOutputStream Used to serialize objects.

DataInputStream and DataOutputStream Read / write primitive Java data types in a machine-independent format.

subclass of filter stream

Page 44: Lecture 6.  Java I/O

Java I/O

Transparency No. 44

LineNumberReader, LineNumberInputStream Keeps track of line numbers while reading.

PushbackReader, PushbackInputStream Two input streams each with a 1-character (or byte) pushback buffer. Sometimes, when reading data from a stream, you will find it useful to

peek at the next item in the stream in order to decide what to do next. However, if you do peek ahead, you'll need to put the item back so that it can be read again and processed normally.

PrintWriter, PrintStream Contain convenient printing methods. public void print(Type), and println([Type]), where the argument Type c

an be any type, either primitive or not. These are the easiest streams to write to, so you will often see other wr

itable streams wrapped in one of these.

Page 45: Lecture 6.  Java I/O

Java I/O

Transparency No. 45

How to Concatenate Files using the sequenceInputStream a concatenation utility that sequentially concatenates files together in the

order they are listed on the command line. The main program

import java.io.*;

public class Concatenate {

public static void main(String[] args) throws IOException {

ListOfFiles mylist = new ListOfFiles(args);

// mylist is an enumeration of objects of type InputStream

SequenceInputStream s = new SequenceInputStream(mylist);

int c;

while ((c = s.read()) != -1) System.out.write(c);

s.close();

} }

Page 46: Lecture 6.  Java I/O

Java I/O

Transparency No. 46

ListOfFiles.java

import java.util.*; import java.io.*;public class ListOfFiles implements Enumeration { private String[] listOfFiles; private int current = 0; public ListOfFiles(String[] listOfFiles) { this.listOfFiles = listOfFiles; } public boolean hasMoreElements() { if (current < listOfFiles.length) return true; else return false; } public Object nextElement() { InputStream in = null; if (!hasMoreElements()) throw new NoSuchElementException("NoMoreFiles."); String nextElement = listOfFiles[current]; current++; try { in = new FileInputStream(nextElement); } catch (FileNotFoundException e) { … }; } return in; } }

Page 47: Lecture 6.  Java I/O

Java I/O

Transparency No. 47

The SequenceInputerStream class

public class SequenceInputStream extends InputStream

Constructor Summary SequenceInputStream(Enumeration e)

e is an Enumeration of objects whose runtime type is InputStream.

SequenceInputStream(InputStream s1, InputStream s2)

Method Summary : All overriding those from inputStream. int available(), void close(), int read(), int read(byte[] b [, int off, int len]) …

Page 48: Lecture 6.  Java I/O

Java I/O

Transparency No. 48

Working with Filtered Streams

You attach a filtered stream to another stream to filter the data as it's read from or written to the original stream.

Subclasses of either FilterInputStream or FilterOutputStream: DataInputStream and DataOutputStream : primitive data bytes BufferedInputStream and BufferedOutputStream : LineNumberInputStream : (deprecated) PushbackInputStream : PrintStream :

Subclasses of FilterReader and FilterWriter: BushbackReader, LineNumberReader, …

Note: FilterReader/Writer use a character stream as its underlying stream while FilterInputStream/OutputStream use a byte stream as its underlying stream.

Page 49: Lecture 6.  Java I/O

Java I/O

Transparency No. 49

Writing Your Own Filtered Streams

Steps Create a subclass of FilterInputStream and FilterOutputStream. Override the read and write methods. Override and/or define any other methods that you might need. Make sure the input and output streams work together.

Example: Use Adler32 to implement a CheckedInputStream and CheckedOutputS

tream to ensure that the data read match those written in other program [ now included in java.util.zip package]

Four classes and one interface make up this example program: 1. CheckedOutputStream, CheckedInputStream. 2. The Checksum interface and the Adler32 class compute a checksum

for the streams. 3. The CheckedIOTest class defines the main method for the program.

Page 50: Lecture 6.  Java I/O

Java I/O

Transparency No. 50

CheckedOutputStream

import java.io.*;

public class CheckedOutputStream extends FilterOutputStream {

private Checksum cksum;

public CheckedOutputStream(OutputStream out, Checksum cksum) {

super(out); this.cksum = cksum; }

public void write(int b) throws IOException {

out.write(b); cksum.update(b); }

public void write(byte[] b) throws IOException {

out.write(b, 0, b.length); cksum.update(b, 0, b.length); }

public void write(byte[] b, int off, int len) throws IOException {

out.write(b, off, len); cksum.update(b, off, len); }

public Checksum getChecksum() { // additional functionality

return cksum; }}

Page 51: Lecture 6.  Java I/O

Java I/O

Transparency No. 51

the CheckedInputStream

import java.io.FilterInputStream, java.io.InputStream, java.io.IOException;public class CheckedInputStream extends FilterInputStream { private Checksum cksum ; public CheckedInputStream(InputStream in, Checksum cksum) { super(in); this.cksum = cksum; } public int read() throws IOException { int b = in.read(); if (b != -1) { cksum.update(b); } return b; } public int read(byte[] b) throws IOException { int len; len = in.read(b, 0, b.length); if (len != -1) { cksum.update(b, 0, len); } return len; } public int read(byte[] b, int off, int len) throws IOException { len = in.read(b, off, len); if (len != -1) { cksum.update(b, off, len); } return len; } public Checksum getChecksum() { return cksum; }}

Page 52: Lecture 6.  Java I/O

Java I/O

Transparency No. 52

Checksum and Adler32

public interface Checksum {

public void update(int b);

public void update(byte[] b, int off, int len);

public long getValue(); // return checksum value

public void reset(); }

public class Adler32 implements Checksum {

private int value = 1; // state of the checksum

private static final int BASE = 65521; // largest prime < 65536

// NMAX is the largest n with 255n(n+1)/2 + (n+1)(BASE-1)<= 2^32-1

private static final int NMAX = 5552;

Page 53: Lecture 6.  Java I/O

Java I/O

Transparency No. 53

public void update(int b) { int s1 = value & 0xffff; int s2 = (value >> 16) & 0xffff; s1 += b & 0xff; s2 += s1; value = ((s2 % BASE) << 16) | (s1 % BASE); }public void update(byte[] b, int off, int len) { int s1 = value & 0xffff; int s2 = (value >> 16) & 0xffff; while (len > 0) { int k = len < NMAX ? len : NMAX; len -= k; while (k-- > 0) { s1 += b[off++] & 0xff; s2 += s1; } s1 %= BASE; s2 %= BASE; } value = (s2 << 16) | s1; }public void reset() { value = 1; }public long getValue() { return (long) value & 0xffffffff; }}

Page 54: Lecture 6.  Java I/O

Java I/O

Transparency No. 54

The test

import java.io.*;public class CheckedIOTest { public static void main(String[] args) throws IOException { Adler32 inChecker = new Adler32(); Adler32 outChecker = new Adler32(); CheckedInputStream in = null; CheckedOutputStream out = null; try { in = new CheckedInputStream( new FileInputStream(“input.txt"), inChecker); out = new CheckedOutputStream(new FileOutputStream("output.txt"), outChecker); } catch (FileNotFoundException e) { … } } catch (IOException e){…} } int c; while ((c = in.read()) != -1) out.write(c); System.out.println("Input stream check sum: " + inChecker.getValue()); System.out.println("Output stream check sum: " + outChecker.getValue()); in.close(); out.close(); }}

Page 55: Lecture 6.  Java I/O

Java I/O

Transparency No. 55

The java.io.DataInputStream class

public class DataInputStream extends FilterInputStream implements DataInput

// allow to read primitive data in machine indep. way Constructor Summary

DataInputStream(InputStream in) Method Summary

int read(byte[] b [, int offset , int length ] ) // read to array b void readFully(byte[] [int,int]) // throws EOFException if not fully read <Type> read<Type>( ) // read one data item <Type> is one of Boolean, Byte, Short, Int, Long, Float, Double String readUTF() // read a string in UTF8 format (instead of

unicode/native) String readLine() // read next line, deprecated int readUnsigned<Type>() // <Type> is either Byte or Short int skipBytes(int n) // always returns n if no exception

Page 56: Lecture 6.  Java I/O

Java I/O

Transparency No. 56

java.io.DataOutputStream public class DataOutputStream extends FilterOutputStream implements Da

taOutputConstructor Summary DataOutputStream(OutputStream out) Method summary void flush(); int size() // return # of bytes written into this stream void write(byte[] b [, int off, int len ]) void write<Type>(<Type> v) throws IOException;

<Type> is any of Int, Long, Float, Double. void write<Type>(int v) throws IOException;

<type> is one of Boolean, Byte, short or Char. void write<Type>( String s)

<Type> is either Bytes or Chars; for Bytes only low-bytes of s are written Writes out the string to the underlying output stream as a sequence of bytes or chars.

void writeUTF(String str) Write str in UTF8 format; first 2 byte = #bytes written != str.size() in general.

Page 57: Lecture 6.  Java I/O

Java I/O

Transparency No. 57

How to Use DataInputStream and DataOutputStream

import java.io.*;public class DataIOTest { public static void main(String[] args) throws IOException { // write the data out DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt")); double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 }; String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" }; for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeChar('\t'); out.writeInt(units[i]); out.writeChar('\t'); out.writeChars(descs[i]); out.writeChar('\n'); } out.close();

Page 58: Lecture 6.  Java I/O

Java I/O

Transparency No. 58

// read it in againDataInputStream in = new DataInputStream(new FileInputStr

eam("invoice1.txt")); double price; int unit; String desc; double total = 0.0; try { while (true) { price = in.readDouble(); in.readChar(); // throws out the tab unit = in.readInt(); in.readChar(); // throws out the tab desc = in.readLine(); System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price); total = total + unit * price; } } catch (EOFException e) { } System.out.println("For a TOTAL of: $" + total); in.close(); }}

Page 59: Lecture 6.  Java I/O

Java I/O

Transparency No. 59

How read/writeUFT8() work

Note: There is no writeUnsignedByte() or writeUnsignedShort() in DataOutputStream.

writeByte(-128) => readByte() = -128 and

radUnsignedByte() = 255. Modified UTF8 format (also used in .class for representing string

s) '\u0001' ~ '\u007F' represented by a single byte: 0xxx xxxx (1~127) ‘\u0000’ and '\u0080' ~ '\u07FF' : 110x xxxx 10xx xxxx (0,128~

2047) '\u0800' ~ '\uFFFF' : 1110 xxxx 10xxxxxx 10xxxxxx (2048~65535) Note: \u0000 is represented by 2 bytes:1100 0000 1000 0000 instead of 0000 0000. when writeUTF(String) the first 2 bytes are an unsigned short indicating t

he number of bytes required to write the string.

Page 60: Lecture 6.  Java I/O

Java I/O

Transparency No. 60

File Management

Have learned how to read/write data from a file, But how about file management besides read/write ?

create new file/directory, set/get file properties (access permission, last update date, owner, file

name, full path etc.)

The class java.io.File Represents a file on the native file system. encapsulates the functionality that you will need to work with the file sy

stem on a machine. You can create a File object for a file on the native file system and then

query the object for information about that file (such as its full pathname).

Page 61: Lecture 6.  Java I/O

Java I/O

Transparency No. 61

Path Name vs Abstract path name

Pathname [strings] are system-dependant strings for identifying directories or files in a system, while abstract pathnames are system independent strings for identifying directories or files.

pathname = sys-dep-prefix ( DirName FileSpearator)*

[ DirName | FileName ] FileSeparator is ‘/’ for UNIX and ‘\’ for WIN32. Default determined by “file.separator” system property; given at field File.separator

abstract pathname = [ sys-dep-prefix ] DirName * [ DirName | FileName ] where sys-dep-prefix is an optional system-dependent prefix string, Unix: => “/” for UNIX root directory. win32: => “Driver:”, “Driver:\" , or "\\" for a Win32 UNC(universal Naming Convention)pathname, and A sequence of zero or more string names.

Each name in an abstract pathname except for the last denotes a directory; the last name may denote either a directory or a file.

Page 62: Lecture 6.  Java I/O

Java I/O

Transparency No. 62

relative vs absolute pathnames

Pathnames and abstract pathname can be either absolute or relative.

An absolute pathname contains full information to locate the file or directory that it denotes.

A relative pathname, in contrast, must be interpreted in terms of information taken from some other base directory pathname.

absolute_pathname = base_directory relative_pathname where by default base_directory is the current user directory, which

can be got by the system property “user.dir”.

Page 63: Lecture 6.  Java I/O

Java I/O

Transparency No. 63

jva.io.File

public class File extends Object implements Serializable, Comparable

File constructors : File(String pathname) // pathname can be relative or absolute

Creates a new File instance by converting the given system dependant pathname string into an system independent abstract pathname.

File(File dir, String child) // child is interpreted as relative Creates a new File instance from a parent abstract pathname and a chil

d pathname string. f1 = new File(“a/b/”, “c:\\d\\e”) ; // returns a\b\c:\d\e, / doesn’t care!

File(String parentDir, String child) Creates a new File instance from a parent pathname string and a child

pathname string.

Page 64: Lecture 6.  Java I/O

Java I/O

Transparency No. 64

File constructor examples

// create File using absolute pathnameFile barDir = new File( “C:\\tmp\\bar" ); // bar need not exist in advance!barDir.exists(); // return false iff C:\temp\bar does not exist.File fooFile = new File( “/tmp/foo.txt" ); // ok for WIN32 and UNIXbarFile.getPath(); // return \tmp\foo.txt for WIN32

// create a file with a relative path : File f1 = new File( "foo" ); // f.getPath() returns “foo”.String dir = System.getProperty(“user.dir”); // suppose it returns “c:\java”File f2 = new File( dir, “foo”); // f2.getPath() returns c:\java\fooFile userDir = new File( dir );File f3 = new File(userDir, “foo”);f2.equals(f3); // return truef2.equals(f1); // return falsef2.equals( f1.getAbsoluteFile()); // return true

Page 65: Lecture 6.  Java I/O

Java I/O

Transparency No. 65

Field Summary

static String pathSeparator The system-dependent path-separator character, represented as a stri

ng for convenience. ( = “path.separator” system property) ; for WIN32 and : FOR UNIX

static char pathSeparatorChar The system-dependent path-separator character.

static String separator The system-dependent default name-separator character, represented

as a string for convenience. ( = “file.separator” system property) \ for WIN32 and / for UNIX

static char separatorChar The system-dependent default name-separator character.

Page 66: Lecture 6.  Java I/O

Java I/O

Transparency No. 66

Method Summary // class methods static File createTempFile(String prefix, String suffix [, File dir])

Creates an empty file in the dir directory, using the given prefix and suffix to generate its name.

suffix == null =>use “tmp” as default; | prefix | > 2. dir = null or not given => use system property “java.io.tmpdir”, which is /tmp or /var/t

mp on UNIX and c:\temp on WIN32 static File[] listRoots()

List the available filesystem roots.

Ex:File[] fs = File.listRoots();for ( int i = 0; i < fs.length(); i++) System.out.println(fs[i].getPath()); the output:>A:\>C:\>D:\

Page 67: Lecture 6.  Java I/O

Java I/O

Transparency No. 67

instance methods

// File properties getter/setter boolean exists(), isDirectory(), isFile() , isHidden() boolean isAbsolute()

Tests whether this abstract pathname is absolute. boolean canRead(), canWrite() // current application can read/write ? long length() // the length of the file denoted by this; 0 for directory. long lastModified()

Returns the time that the file denoted by this abstract pathname was last modified. boolean setLastModified (long time) boolean setReadOnly() boolean equals(Object obj)

Tests this abstract pathname for equality with the given object. int hashCode()

Computes a hash code for this abstract pathname. int compareTo(File pathname), compareTo(Object o)

Compares two abstract pathnames lexicographically.

Page 68: Lecture 6.  Java I/O

Java I/O

Transparency No. 68

pathname/ File transformations File getAbsoluteFile() ; String getAbsolutePath() ;

Returns the absolute pathname string of this abstract pathname. File getCanonicalFile() ; String getCanonicalPath() ;

Returns the canonical form of this abstract pathname. new File(“/a/b/../”).getCanonicalPath() // return C:\a but C:\a\b\.. for getAbsolutePth()

String getParent() ; File getParentFile() Returns the pathname string or abstract pathname of this abstract pathname's paren

t, or null if this pathname does not name a parent directory. String getName() // Returns the name of the file/directory denoted by this. String getPath() // File pathname String

Converts this abstract pathname into a pathname string. boolean renameTo(File dest)

Renames the file denoted by this abstract pathname. String toString() : Returns the pathname string of this abstract pathname.

URL toURL() Converts this abstract pathname into a file: URL. c:\a\b\c.txt file:/c:/a/b/c.txt \\host\sh\b\c.txt file://host/sh/b/c.txt

Page 69: Lecture 6.  Java I/O

Java I/O

Transparency No. 69

Directory and File operations

// File operations boolean createNewFile() // atomically check and create new file if not exis

ting. boolean delete() // delete this file or directory void deleteOnExit() // delete when on system exits

// directory operations boolean mkdirs() , mkdir()

Creates the directory named by this Fie, mkdirs() will also create any necessary nonexistent parent directories.

String[] list(), list(FilenameFilter filter) Returns an array of strings naming the files and directories in the directory denoted

by this abstract pathname that satisfy the specified filter.

File[] listFiles(), listFiles(FileFilter filter), listFiles(FilenameFilter filter) Returns an array of abstract pathnames denoting the files and directories in the dire

ctory denoted by this abstract pathname that satisfy the specified filter.

Page 70: Lecture 6.  Java I/O

Java I/O

Transparency No. 70

FilenameFilter and FileFilter

public interface FilenameFilter {public boolean accept(File dir, String name); }

public interface FileFilter { public boolean accept(File dir); } Implementation of listFiles(FilenameFilter)public File[] listFiles( FilenameFilter filter) {

String ss[] = list(); if (ss == null) return null;ArrayList v = new ArrayList();for (int i = 0 ; i < ss.length ; i++) if (( filter == null) || filter.accept(this, ss[i]) )

v.add(new File( this.getpath() , ss[i])); return (File[])(v.toArray(new File[0]));

}

Page 71: Lecture 6.  Java I/O

Java I/O

Transparency No. 71

Example

Find all java source files lastly modified in one day in current user directory

File userDir = new File (System.getProperty(“user.dir”));int now = new Date().getTime(); // now is current time in ms.String[] rlts = dir.list( new FilenameFilter(){ boolean accept( File dir, String name) { File f = new File (dir, name); // dir bound to this at runtim

e return name.endWith(“.java”) && (now - f.lastModified() < 1000*60*60*24) ; } });for(int i = 0; i < rlts.length; i++) System.out.println(rlts[i]);

Page 72: Lecture 6.  Java I/O

Java I/O

Transparency No. 72

Object Serialization

The process of reading and writing objects is called object serialization

ObjectInputStream and ObjectOutputStream are streams used to read/write objects.

when to use object serialization: Remote Method Invocation (RMI)--communication between objects via

sockets Lightweight persistence--storage of an object for use in a later invocati

on of the same program What a Java programmer need to know about object serializatio

n: How to serialize objects by writing them to an ObjectOutputStream and

reading them in again using an ObjectInputStream. how to write a class so that its instances can be serialized.

Page 73: Lecture 6.  Java I/O

Java I/O

Transparency No. 73

Serializing Objects

How to Write to an ObjectOutputStream ? straight-forward! Ex:

FileOutputStream out = new FileOutputStream("theTime");

ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject("Today");

s.writeObject(new Date());

s.flush();

Page 74: Lecture 6.  Java I/O

Java I/O

Transparency No. 74

Notes about ObjectOutputStreams

If an object refers to other objects, then all of the objects that are reachable from the first must be written at the same time

ObjectOutputStream stream implements the DataOutput interface that defines many methods for writing primitive data types, such as writeInt, writeFloat, or writeUTF. You can use these methods to write primitive data types to an ObjectO

utputStream.

The writeObject method throws a NotSerializableException if it's given an object that is not serializable. An object is serializable only if its class implements the Serializable int

erface.

Page 75: Lecture 6.  Java I/O

Java I/O

Transparency No. 75

How to Read from an ObjectInputStream

EX:

FileInputStream in = new FileInputStream("theTime");

ObjectInputStream s = new ObjectInputStream(in);

String today = (String)s.readObject();

Date date = (Date)s.readObject();

ObjectInputStream stream implements the DataInput interface that defines methods for reading primitive data types. Use these methods to read primitive data types from an ObjectInputStream.

Page 76: Lecture 6.  Java I/O

Java I/O

Transparency No. 76

The java.io.datainput interface

public abstract interface java.io.DataInput { // Methods

public abstract <Type> read<TYPE>();

public abstract void readFully(byte[] b [, int off, int len ] );

public abstract String readLine();

public abstract int readUnsignedByte();

public abstract int readUnsignedShort();

public abstract String readUTF();

public abstract int skipBytes(int n);}

Page 77: Lecture 6.  Java I/O

Java I/O

Transparency No. 77

The java.io.ObjectInput Interface

public abstract interface java.io.ObjectInput extends java.io.DataInput {

// Methods

public abstract int available();

public abstract void close();

public abstract int read();

public abstract int read(byte[] b);

public abstract int read(byte[] b, int off, int len);

public abstract Object readObject();

public abstract long skip(long n);

}

Page 78: Lecture 6.  Java I/O

Java I/O

Transparency No. 78

java.io.ObjectInputStream

public class java.io.ObjectInputStream extends java.io.InputStream implements java.io.ObjectInput { // Constructors public ObjectInputStream(InputStream in); // Public Instance Methods // implementation of ObjectInput public final void defaultReadObject();

// Read the non-static and non-transient fields of the current class from this stream.

public synchronized void registerValidation(ObjectInputValidation obj, int prio);// Protected Instance Methods protected final boolean enableResolveObject(boolean enable); protected void readStreamHeader(); protected Class resolveClass(ObjectStreamClass v); protected Object resolveObject(Object obj);}

Page 79: Lecture 6.  Java I/O

Java I/O

Transparency No. 79

The DataOutput interface

public abstract interface java.io.DataOutput { // Methods

public abstract void write(byte[] b);

public abstract void write(byte[] b, int off, int len);

public abstract void write(int b);

public abstract void write<TYPE>(<Type> v);

where <TYPE> is any of Boolean, Byte, Char, Short, Int, Long, Float, Double., and corresponding <Type> is int, int, int, int, long, float and double.

public abstract void writeUTF(String str);

}

Page 80: Lecture 6.  Java I/O

Java I/O

Transparency No. 80

The DataOutput Stream

public abstract interface java.io.ObjectOutput extends java.io.DataOutput {

// Methods

public abstract void close();

public abstract void flush();

public abstract void write(int b);

public abstract void write(byte[] b);

public abstract void write(byte[] b, int off, int len);

public abstract void writeObject(Object obj);

}

Page 81: Lecture 6.  Java I/O

Java I/O

Transparency No. 81

the ObejctOutputStream

public class java.io.ObjectOutputStream extends java.io.OutputStream implements java.io.ObjectOutput { // Constructors public ObjectOutputStream(OutputStream out); // Instance Methods // implementations of ObejctOutput + public final void defaultWriteObject(); public void flush(); public void reset();// Protected Instance Methods protected void annotateClass(Class cl); protected void drain(); protected final boolean enableReplaceObject(boolean enable); protected Object replaceObject(Object obj); protected void writeStreamHeader();}

Page 82: Lecture 6.  Java I/O

Java I/O

Transparency No. 82

Providing Object Serialization for Your Classes

Implementing the Serializable Interface :

package java.io; public interface Serializable {};

Customizing Serialization Implementing the Externalizable Interface Protecting Sensitive Information

Page 83: Lecture 6.  Java I/O

Java I/O

Transparency No. 83

RandomAccessFile class

reads data from and writes data to a file. The file is specified using a File object or a String that represent

s a pathname. constructors take a mode parameter that specifies whether the f

ile is being opened solely for reading, or for reading and writing. can throw a SecurityException if the application does not have

permission to access the specified file using the given mode. supports random access to the data in the file; the seek() method allows you to alter the current position of the

file pointer to any location in the file. Implements both the DataInput and DataOutput interfaces, so it

supports reading and writing of all the primitive data types.

Page 84: Lecture 6.  Java I/O

Java I/O

Transparency No. 84

class summary

public class java.io.RandomAccessFile extends java.lang.Object implements java.io.DataInput, java.io.DataOutput { // Constructors; mode is either “r” or “rw” public RandomAccessFile(File file, String mode); public RandomAccessFile(String name, String mode);

if “rw” mode and fie not exists => create it!

// Instance Methods // implementatin of DataInput and DataOutput + public final FileDescriptor getFD(); public native long getFilePointer(); public native long length(); pubilc void setLength(int); // set the length of the file, resulting in extension/t

runcation of the file content. public native void seek(long pos);}