Transcript Lecture ?. Java I/O
Java Programming
Lecture 6. Java I/O
Cheng-Chia Chen
Transparency No. 1
Java I/O 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 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
write information to an
output stream
and
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 // open
2. while there is more information // reading
read next data from the stream
// close
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 // 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 many 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
The java.io package
InputStream OutputStream Reader Writer
Java I/O Transparency No. 7
java.io.InputStream and its subclasses gray => data sink stream white => processing stream // require other streams Java I/O Transparency No. 8
The Java.io.OutputStream and its subclasses Java I/O Transparency No. 9
java.io.Reader and its subclasses Java I/O Transparency No. 10
java.io.Writer and its subclasses Java I/O 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: characters and arrays of
int read() // read one char, casted as an int.
int read(char cbuf[]) // read chars into cbuf, return #chars read.
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 standard output
– – defined by System.in
defined by System.out
standard error
– defined by System.err
We use System.out
when we execute println System.out and System.err are of the type PrintStream.
statements
System.in
is declared to be a generic reference, and therefore usually must be mapped to a more useful stream with specific characteristics InputStream
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 stream. and releases any system resources associated with the
abstract int
read ( [ Reads the next byte byte[] b [ , int offset, int length of data from the input stream.
]] )
Reads some number of bytes array b. from the input stream and stores them into the buffer
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 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) void write(String str)
// Write a single character.
// 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:
Sink type character stream byte stream array String Pipe File Input Output Input Output Input Output Input Output CharArrayReader, CharArrayWriter ByteArrayInputStream, ByteArrayOutputStream StringReader, StringWriter (deprecated) PipedReader, PipedWriter PipedOutputStream
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:
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 an 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 ] the written String ) // initial buffer size; use toString() to get StringBufferInputStrean(String) // deprecated ! 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:
f = new
(
); where
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) out.write(c); in.close(); out.close(); 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 (words) pipe 1 pipe 2 sink (rhymedWords)
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) 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; }
in(source) reverse Thread pipeOut out(pipeOut) pipeIn
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() if (out != null && in != null) { of a java program try { String input ; while (( input = in.readLine()) != null) { out.
println (reverseIt( input )); out.flush(); } out.close(); } catch (IOException e) { err.println("ReverseThread run: " + e); } } } private String reverseIt (String source) { …} // reverse 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 0) hi--; if (lo < hi) { String T = a[lo]; lo++; a[lo] = a[hi]; hi--; a[hi] = T; } } 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); ...
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 method.
is a [file | pipe] Reader, which does not provide the readLine()
PrintWriter out = new PrintWriter(pipeOut); pipeOut is a [piped] writer, which does not provide the convenient println() method. Transparency No. 38
3.1 Types of the Processing Streams process Buffering character stream byte stream BufferedReader, BufferedWriter BufferedOutputStream Java I/O Filtering FilterReader, FilterWriter FilterInputStream, FilterOutputStream B2C and C2B Concatenation Object Serialization Data Conversion Counting printing Peeking Ahead InputStreamReader, OutputStreamWriter (deprecated) LineNumberReader PrintWriter PushbackReader SequenceInputStream ObjectInputStream, ObjectOutputStream DataInputStream, DataOutputStream LineNumberInputStream PrintStream 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 program raw Output Stream buffer BufferedInputStream 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 program underlying Output Stream Transformation + additional functionality FilterInputStream 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 program OutputStream InputStreamReader OutputStreamWriter encoding 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() if (!hasMoreElements()) { InputStream in = null; 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 BufferedInputStream DataOutputStream : primitive data
and BufferedOutputStream : bytes
LineNumberInputStream : (deprecated) PushbackInputStream : PrintStream : Subclasses of FilterReader and FilterWriter:
BushbackReader, LineNumberReader, … Note: FilterReader/Writer use a underlying stream while FilterInputStream/OutputStream use a byte stream character stream as its underlying stream.
as its 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 CheckedOutputStream CheckedInputStream written in other program [ now included in and to ensure that the data read match those java.util.zip
package]
Four classes and one interface make up this example program: 1. CheckedOutputStream , CheckedInputStream. 2. The Checksum for the streams. 3. The interface and the CheckedIOTest Adler32 class compute a checksum 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(); public int read(byte[] b) if (b != -1) { cksum.update(b); throws IOException { } return b; } 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) len = in.read(b, off, len); throws IOException { if (len != -1) { cksum.update(b, off, len); public Checksum getChecksum() } return len; } { 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
implements
DataInput
extends FilterInputStream // 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 ] )
Java I/O java.io.DataOutputStream
public class DataOutputStream DataOutput extends FilterOutputStream implements Constructor Summary
DataOutputStream(OutputStream out)
Method summary
void flush(); int size() void write(byte[] b void write
void write
void write
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 readUnsignedByte() = 128.
Modified UTF8 format
strings)
'\u0001'
~
'\u007F '
(also used in .class for representing represented by a single byte: 0xxx xxxx (1~127)
‘
\u0000
’
'\u0800' and '\u0080'
~
'\uFFFF'
: ~
'\u07FF'
: 110x xxxx 10xx xxxx 1110 xxxx 10xxxxxx 10xxxxxx (0,128~2047) (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 where sys-dep-prefix is an optional system-dependent prefix string, | FileName ]
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 relative
. or 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 child pathname string .
and a 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 pathname string.
and a child 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”); File f2 = new File( dir, “foo”); // f2.getPath() // suppose it returns “ returns c:\java\foo c:\java ” 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
pathSeparator
Char
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
separator
Char
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 /var/tmp on UNIX and c:\temp on WIN32 /tmp or 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() long length() long lastModified()
// current application can read/write ?
// the length of the file denoted by this; 0 for directory.
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() existing. boolean delete() // atomically check and create new file if not // 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() nonexistent parent directories.
will also create any necessary
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 String name); } {public boolean accept(File dir,
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 to read/write objects.
ObjectOutputStream
are streams used 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.writeInt( 12345 ); 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 types, such as DataOutput interface that defines many methods for writing primitive data
writeInt, writeFloat, or writeUTF
.
You can use these methods to write primitive data types to an ObjectOutputStream. The writeObject method throws a
NotSerializableException
given an object that is not serializable . if it's
An object is serializable interface. only if its class implements the Serializable Serializable is a marker interface: you just declare you implement it but do not need to implement any additional method.
Transparency No. 74
EX: How to Read from an ObjectInputStream FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); int d = s.readInt(); String today = (String)s.readObject(); Date date = (Date)s.readObject(); Java I/O
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.
The order and types of objects read in must be the same as they were written out using ObjectOutputStream. Transparency No. 75
Java I/O The java.io.datainput interface public abstract interface java.io.DataInput { // Methods public abstract
read
(); //
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.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 readObject(), readInt() ,… Java I/O // Read the non-static and non-transient fields of the current class from this stream. may only be called by readObject() of the object to be deserialized.
public final void defaultReadObject(); public synchronized void registerValidation( ObjectInputValidation obj, int prioity);
ObjectInputValidation.validateObject() can be used to validate read-in object.
There can be more than one validation objects registered, each called back according to prioity ( 0
first ).
See this reference for an explanation of objectInputValidation Transparency No. 78
Java I/O } // Protected Instance Methods protected final boolean enableResolveObject(boolean enable); protected void readStreamHeader(); protected Class resolveClass(ObjectInputStreamClass v); protected Object resolveObject(Object obj); Transparency No. 79
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
(
v); } where
is any of Boolean, Byte, Char, Short, Int, Long, Float, Double ., and corresponding
is int, int, int, int, long, float and double .
public abstract void writeUTF(String str); 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); Java I/O Transparency No. 81
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; implementation of ObjectOutput.
….
// Write the non-static and non-transient fields of the current class to this stream. may only be called by writeObject() of the object to be serialized.
public final void defaultWriteObject(); public void flush(); public void reset(); // Protected Instance Methods; used only if you need to extend this class.
protected void annotateClass(Class cl); protected void drain(); protected final boolean enableReplaceObject(boolean enable); protected Object replaceObject(Object obj); protected void writeStreamHeader();} Transparency No. 82
Providing Object Serialization for Your Classes Implementing the Serializable Interface : package java.io; public interface Serializable {}; Java I/O
Customizing Serialization Implementing the Externalizable Interface Protecting Sensitive Information Transparency No. 83
Java I/O Customizing Serialization The default serialization mechanism (ObjectInputStream.readXXX() + ObjectOutputStream.writeXXX()) will read/write the type and content of every target object, and recursively all types and contents of all subobjects.
Sometimes it is needless to read/write all fields of an object:
ex: public class Point { int x,y; float r, theta; }
For point object, we may want to save x and y only.
Solution: use the
transient
modifier : pubilc class Point {int x,y; transient float r, theta ; } Every transiant field will not be read/written by during object serialization.
Problem: How to recover r, theta
Customizing serialization!!
from read x and y ?
Transparency No. 84
public class Point implement Serializable { int x,y;
transiant
float r, theta }
private readObject(ObjectInputStream oin ) { oin.defaultReadObject() ;
// this will set x and y only.
r = Math.sqrt( x * x + y * y ); theta = (float ) Math.arcTan( (double) y / x ); }
Java I/O Transparency No. 85
Java I/O More Example public class LabeledPoint implements Serializable { private String label; private transient Point2D.Double
point; private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); // this will write label only!!
out.writeDouble(point.getX()); // manually write contents of point out.writeDouble(point.getY()); } private void readObject(ObjectInputStream in) throws IOException { in.defaultReadObject(); // this will set label only!
double x = in.readDouble(); double y = in.readDouble(); // manually read previously written data!
point = new Point2D.Double(x, y); // reconstruct point!
} } Transparency No. 86
Java I/O Read/Write Externalizable objects Serialization is somewhat slow because the JVM must discover the structure of each object. If you are concerned about performance and if you need to read /write a large number of objects of a particular class, you should use the Externalizable interface. public interface Externalizable { public void readExternal(ObjectInput ) ; pubilc void writeExternal(ObjectOutput);
} Notes:
Target class should implement this interface.
both read/write methods are public.
should also read/write ancestor classes data ObjectStream will use either method if they can find them in target classes.
Transparency No. 87
Java I/O Example public class Employee implements Serializable, Externalizable { String name; double salary; s.writeUTF(name); s.writeDouble(salary); transiant Date hireDay ; public void writeExternal(ObjectOutput s ) { s.writeLong( hireDay.getTime() ); } public void readExternal(ObjectInput s) throws IOException { name = s.readUTF(); salary = s.readDouble(); hireDay = new Date(s.readLong()); }}
e = new Employee(); … ; out = new ObjectOutputStream(…); out.writeObject(e) ; … ; in = new ObjectInputStream(…) ; Employee e1 = in.readObject(); assert e.equlas(e1); Transparency No. 88
References for Java Object Serialization Java Object Serialization Specification ObjectInputStream ObjectOutputStream Serialize objects not serializable Serialization and magic methods Java I/O Transparency No. 89
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. 90
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); Java I/O Transparency No. 91