Lecture ?. Java I/O

Download Report

Transcript Lecture ?. Java I/O

Java Programming
Lecture 6.
Java I/O
Cheng-Chia Chen
Transparency No. 1
Java I/O
Contents
1.
2.
Overview of I/O Streams
Using the Data Sink Streams
1.
2.
3.
How to Use File Streams
How to Use Pipe Streams
Using the Processing Streams
1.
2.
How to Concatenate Files
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.
2.
Using Random Access Files
Writing Filters for Random Access Files
Transparency No. 2
Java I/O
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
Transparency No. 3
Java I/O
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
2. while there is more information
read next data from the stream
3. close the stream.
// for writing
1. open an output stream
2. while there is more information
write data to the stream
3. close the stream.
// open
// reading
// close
// open
// writing
// close
Transparency No. 4
Java I/O
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
Transparency No. 5
Java I/O
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
Transparency No. 6
Java I/O
The java.io package
InputStream
OutputStream
Reader
Writer
Transparency No. 7
Java I/O
java.io.InputStream and its subclasses
 gray => data sink stream
 white => processing stream // require other streams
Transparency No. 8
Java I/O
The Java.io.OutputStream and its subclasses
Transparency No. 9
Java I/O
java.io.Reader and its subclasses
Transparency No. 10
Java I/O
java.io.Writer and its subclasses
Transparency No. 11
Java I/O
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.
Transparency No. 12
Java I/O
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.
Transparency No. 13
Java I/O
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 PrintStream.
 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);
Transparency No. 14
Java I/O
java.io.InputStream
 public abstract class InputStream extends Object
Constructor :
 InputStream() // default no-arg constuctor
Method 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
stream.
 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
array b.
 long skip(long n)
 Skips over and discards n bytes of data from this input stream.
Transparency No. 15
Java I/O
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
this mark operation.
 void reset()
 Repositions this stream to the position at the time the mark method
was last called on this input stream.
 boolean markSupported()
 Tests if this input stream supports the mark and reset methods.
Transparency No. 16
Java I/O
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.
Transparency No. 17
Java I/O
Methods summary of java.io.Reader
 abstract void close() // Close the stream.
 int read() // Read a single character.
 abstract 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.
Transparency No. 18
Java I/O
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
stream.
 abstract void write( int b) // Writes the specified byte to this output stream.
Transparency No. 19
Java I/O
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 higher
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.
Transparency No. 20
Java I/O
java.io.Writer method summary
 abstract void
 abstract void
 abstract void
close() // Close the stream, flushing it first.
flush() // Flush the stream.
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.
Transparency No. 21
Java I/O
JDK ‘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.
Transparency No. 22
Java I/O
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
array
CharArrayReader, ByteArrayInputStream,
CharArrayWriter ByteArrayOutputStream
String
StringReader,
StringWriter
StringBufferInputStream
(deprecated)
Pipe
PipedReader,
PipedWriter
PipedInputStream,
PipedOutputStream
File
FileReader,
FileWriter
FileInputStream,
FileOutputStream
Transparency No. 23
Java I/O
Summary of the data sink streams
 FileReader, FileWriter; FileInputStream, FileOutputStream
 Collectively called file streams; used to read from or write to a file on
the 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
write methods to read from or write to the array.
 Constructors: <Type>( {char | byte } [] [, int offset, int length ] )
 Ex: CharArrayWriter cwr = new CharArrayWriter( new char[100] ) ;
 CharArrayWriter cw = new CharArrayWriter( new char[1024], 256,512 ) ;
 ByteArrayInputstream bi = new ByteArrayInputStream

( new byte[] {\011,\022, \0xff} );
Transparency No. 24
Java I/O
String Streams
 StringReader, StringWriter, StringBufferInputStream
 StringReader : used to read characters from a String.
 StringWriter : used to write to a String. StringWriter collects the
characters 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
the written String
 StringBufferInputStrean(String) // deprecfated ! Only the low eight
bits of each character in the string are used by this class.
Transparency No. 25
Java I/O
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.
Transparency No. 26
Java I/O
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, and
FileOutputStream and
 <file> is either a string for file name, a File object, or a FileDescriptor
object (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
out );
Transparency No. 27
Java I/O
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();
} }
Transparency No. 28
Java I/O
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)
in.close();
out.close();
out.write(c);
}}
Transparency No. 29
Java I/O
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.
program p2: output
p : a pipe
input of program p1
 for p2, p is an PipedWriter ( or PipedOutputStream )
 for p1, p is a PipedReader ( or PipedInputstream )
Transparency No. 30
Java I/O
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.
sink
(rhymedWords)
sink
(words)
pipe 1
pipe 2
Transparency No. 31
Java I/O
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();
}
Transparency No. 32
Java I/O
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; }
pipeOut
in(source)
reverse Thread
pipeIn
out(pipeOut)
Transparency No. 33
Java I/O
// source is a PipedReader
public 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;
}
}
Transparency No. 34
Java I/O
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) { …}
}
Transparency No. 35
Java I/O
SortThread.java
import 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);
}} }
Transparency No. 36
Java I/O
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);
}}
}
Transparency No. 37
Java I/O
3. Using the processing streams
Using Streams to Wrap Other Streams :
 The reverse method contains some other interesting code; in
particular, 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 readLine()
method.
 pipeOut is a [piped] writer, which does not provide the convenient
println() method.
Transparency No. 38
Java I/O
3.1 Types of the Processing Streams
process
character stream
byte stream
Buffering
BufferedReader,
BufferedWriter
BufferedInputStream,
BufferedOutputStream
Filtering
FilterReader,
FilterWriter
FilterInputStream,
FilterOutputStream
B2C and C2B
InputStreamReader,
OutputStreamWriter
StringBufferInputStream
(deprecated)
Concatenation
SequenceInputStream
Object Serialization
ObjectInputStream,
ObjectOutputStream
Data Conversion
DataInputStream,
DataOutputStream
Counting
LineNumberReader
LineNumberInputStream
printing
PrintWriter
PrintStream
Peeking Ahead
PushbackReader
PushbackInputStream
Transparency No. 39
Java I/O
3.2 Summary of the processing streams
 Buffered Streams
 Buffer data while reading or writing, thereby reducing the number of
accesses required on the original data source.
 typically more efficient than similar nonbuffered streams; suggest
always 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
buffer
BufferedInputStream
program
raw Output Stream
BufferedOutputStream
buffer
Transparency No. 40
Java I/O
Filter Streams
 Filter streams
 contains some other input stream as its basic source of data, possibly
transforming the data along the way or providing additional
functionality.
(InputStream/Writer etc)
 Abstract classes, same as their parent streams (without new methods).
 They define the interface for filter streams, which filter data as it's
being read or written.
underlying Input Stream
Transformation +
additional
functionality
FilterInputStream
program
underlying Output Stream
transformation +
additional
functionality
FilterOutputStream
Transparency No. 41
Java I/O
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
encoding
program
InputStreamReader
OutputStream
OutputStreamWriter
decoding
Transparency No. 42
Java I/O
 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
Transparency No. 43
Java I/O
 LineNumberReader, LineNumberInputStream
 Keeps track of line numbers while reading.
 PushbackReader, PushbackInputStream
 Two input streams each with a 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
can be any type, either primitive or not.
 These are the easiest streams to write to, so you will often see other
writable streams wrapped in one of these.
 c.f. PrintWriter(wrapping a writer) can not write byte[ ].
Transparency No. 44
Java I/O
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();
}}
Transparency No. 45
Java I/O
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;
} }
Transparency No. 46
Java I/O
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])
 …
Transparency No. 47
Java I/O
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.
Transparency No. 48
Java I/O
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
CheckedOutputStream 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.
Transparency No. 49
Java I/O
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; }}
Transparency No. 50
Java I/O
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; }}
Transparency No. 51
Java I/O
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;
Transparency No. 52
Java I/O
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; }
}
}
Transparency No. 53
Java I/O
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();
}}
Transparency No. 54
Java I/O
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, char
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
Transparency No. 55





Java I/O
java.io.DataOutputStream
 public class DataOutputStream extends FilterOutputStream implements
DataOutput
Constructor 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.
Transparency No. 56
Java I/O
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();
Transparency No. 57
Java I/O
// read it in again
DataInputStream in = new DataInputStream(new
FileInputStream("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();
}}
Transparency No. 58
Java I/O
How read/writeUFT8() work
 Note: There is no
 writeUnsignedByte() or writeUnsignedShort() in DataOutputStream.
 writeByte(-128) => readByte() = -128 and
radUnsignedByte() = 128.
 Modified UTF8 format (also used in .class for representing
strings)






'\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
the number of bytes required to write the string.
Transparency No. 59
Java I/O
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
system 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).
Transparency No. 60
Java I/O
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.
Transparency No. 61
Java I/O
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”.
Transparency No. 62
Java I/O
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
child 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.
Transparency No. 63
Java I/O
File constructor examples
// create File using absolute pathname
File 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 UNIX
barFile.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\foo
File userDir = new File( dir );
File f3 = new File(userDir, “foo”);
f2.equals(f3); // return true
f2.equals(f1); // return false
f2.equals( f1.getAbsoluteFile()); // return true
Transparency No. 64
Java I/O
Field Summary
static String pathSeparator
 The system-dependent path-separator character, represented as a
string 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.
Transparency No. 65
Java I/O
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/tmp 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:\
Transparency No. 66
Java I/O
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.
Transparency No. 67
Java I/O
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 parent,
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
Transparency No. 68
Java I/O
Directory and File operations
// File operations
 boolean createNewFile() // atomically check and create new file if not
existing.
 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
directory denoted by this abstract pathname that satisfy the specified filter.
Transparency No. 69
Java I/O
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]));
}
Transparency No. 70
Java I/O
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 runtime
return name.endWith(“.java”) &&
(now - f.lastModified() < 1000*60*60*24) ; }
});
for(int i = 0; i < rlts.length; i++)
System.out.println(rlts[i]);
Transparency No. 71
Java I/O
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
invocation of the same program
 What a Java programmer need to know about object
serialization:
 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.
Transparency No. 72
Java I/O
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();
Transparency No. 73
Java I/O
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
ObjectOutputStream.
 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
interface.
Transparency No. 74
Java I/O
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.
Transparency No. 75
Java I/O
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);}
Transparency No. 76
Java I/O
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);
}
Transparency No. 77
Java I/O
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);
Transparency No. 78
protected Object resolveObject(Object obj);}
Java I/O
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);
}
Transparency No. 79
Java I/O
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);
}
Transparency No. 80
Java I/O
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();}
Transparency No. 81
Java I/O
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
Transparency No. 82
Java I/O
RandomAccessFile class
 reads data from and writes data to a file.
 The file is specified using a File object or a String that
represents a pathname.
 constructors take a mode parameter that specifies whether the
file 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.
Transparency No. 83
Java I/O
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/truncation of the file content.
public native void seek(long pos);
}
Transparency No. 84