Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6. Java I/O Cheng-Chia Chen.

Similar presentations


Presentation on theme: "Lecture 6. Java I/O Cheng-Chia Chen."— Presentation transcript:

1 Lecture 6. Java I/O Cheng-Chia Chen

2 Overview of I/O Streams Using the Data Sink Streams
Contents Overview of I/O Streams Using the Data Sink Streams How to Use File Streams How to Use Pipe Streams Using the Processing Streams How to Concatenate Files Working with Filtered Streams How to Use DataInputStream and DataOutputStream Writing Your Own Filtered Streams Object Serialization Serializing Objects Providing Object Serialization for Your Classes Working with Random Access Files Using Random Access Files Writing Filters for Random Access Files

3 I/O Streams A stream is a sequence of bytes (or data or objects) that flow from a source to a destination In a program, we read information from an input stream and write information to an output stream for output for input

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

5 An I/O stream is either a
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

6 classification of Java I/O streams and their naming conventions
By I/O Directions: for Input => Input for output => Output By datatype of stream content char data => Reader / Writer byte data => InputStream / OutputStream other primitive data(int, long, float,…) => DataInputStream /DataOutputStream Objects => ObjectInputStream /ObjectOutputStream By characteristic of stream source / destination 1. for final device : data sink stream file, byte/char array, StringBuffer, pipe purpose: serve as the source/destination of the stream. 2. for intermediate process : processing streams Buffering, Filtering, Byte2Char, Char2Byte, etc. purpose: alters or manages information in the stream

7 The java.io package InputStream OutputStream Reader Writer

8 java.io.InputStream and its subclasses
gray => data sink stream white => processing stream // require other streams

9 The Java.io.OutputStream and its subclasses

10 java.io.Reader and its subclasses

11 java.io.Writer and its subclasses

12 Basic methods provided in java input classes
Reader and InputStream define similar APIs but for different data types. Reader contains methods for reading characters and arrays of characters: int read() // 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.

13 Basic methods provided in java output streams
Writer defines these methods for writing character, string and arrays of characters void write(int c) // 16 low-ordered bits of c is written void write(char[] cbuf [, int off, int len ] ) void write(String s [, int off, int len ] ) OutputStream defines the same methods but for bytes: void write(int c) // 8 low-ordered bits of c is written void write(byte cbuf[]) void write(byte cbuf[], int offset, int length) All of the streams--readers, writers, input streams, and output streams— are automatically opened when created ! can be closed by calling its close() method, or the garbage collector. will throw IOException on failure of IO operations.

14 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);

15 public abstract class InputStream extends Object Constructor :
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.

16 Methods for marking operaqtions
void mark(int readlimit) Marks the current position in this input stream. readlimit is the number of bytes that can be read without invalidating 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.

17 public abstract class Reader extends Object
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.

18 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.

19 public abstract class OutputStream extends Object
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.

20 public abstract class Writer extends Object
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.

21 java.io.Writer method summary
abstract void close() // Close the stream, flushing it first. abstract void flush() // Flush the stream. abstract void write(char[] cbuf [, int off, int len]) Write a portion of an array of characters. void write(int c) // Write a single character. void write(String str) // Write a string. void write(String str, int off, int len) Write a portion of a string.

22 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.

23 Types of data sinks: memory[array, strings], files, or pipes
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, CharArrayWriter ByteArrayInputStream, ByteArrayOutputStream String StringReader, StringWriter StringBufferInputStream (deprecated)  Pipe PipedReader, PipedWriter PipedInputStream, PipedOutputStream File FileReader, FileWriter FileInputStream, FileOutputStream Input Output

24 Summary of the data sink streams
FileReader, FileWriter; FileInputStream, FileOutputStream Collectively called file streams; used to read from or write to a file on 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} );

25 StringReader, StringWriter, StringBufferInputStream
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] ) // initial buffer size; use toString() to get the written String StringBufferInputStrean(String) // deprecated ! Only the low eight bits of each character in the string are used by this class.

26 PipedReader, PipedWriter; PipedInputStream, PipedOutputStream
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.

27 File streams are the easiest streams to understand.
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 );

28 A simple file-copy program
Copy the contents of a file named input.txt into a file called output.txt: import java.io.*; public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File(“input.txt"); File outputFile = new File("output.txt"); FileReader in = new FileReader( inputFile ); FileWriter out = new FileWriter( outputFile ); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } }

29 public class CopyBytes {
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(); }}

30 PipedReader, PipedWriter ; PipedInputStream, PipedOutputStream
2.2 How to Use Pipe Streams A pipe is a channel for receiving data from one program (or thread) and sending the received data to the input of another. PipedReader, PipedWriter ; PipedInputStream, PipedOutputStream implement the input and output components of a pipe. for p2, p is an PipedWriter ( or PipedOutputStream ) for p1, p is a PipedReader ( or PipedInputstream ) p : a pipe input of program p1 program p2: output

31 a simple program to reverse, sort and reverse words in a file.
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) sink (rhymedWords) pipe 1 pipe 2

32 public class RhymingWords {
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(); }

33 pipeOut in(source) pipeIn out(pipeOut)
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) pipeIn reverse Thread out(pipeOut)

34 // 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; }

35 public class ReverseThread extends Thread {
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) { err.println("ReverseThread run: " + e); } } } private String reverseIt(String source) { …} // reverse source }

36 public class SortThread extends Thread {
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); } } }

37 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); }}

38 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.

39 3.1 Types of the Processing Streams
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

40 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

41 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. 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. (InputStream/Writer etc) underlying Input Stream program underlying Output Stream FilterInputStream Transformation + additional functionality FilterOutputStream transformation +

42 default encoding from system property : “file.encoding” Ex:
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 encoding decoding OutputStreamWriter

43 ObjectInputStream and ObjectOutputStream
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

44 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[ ].

45 How to Concatenate Files using the sequenceInputStream
a concatenation utility that sequentially concatenates files together in the order they are listed on the command line. The main program import java.io.*; public class Concatenate { public static void main(String[] args) throws IOException { ListOfFiles mylist = new ListOfFiles(args); // mylist is an enumeration of objects of type InputStream SequenceInputStream s = new SequenceInputStream(mylist); int c; while ((c = s.read()) != -1) System.out.write(c); s.close(); } }

46 import java.util.*; import java.io.*;
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; } }

47 The SequenceInputerStream class
public class SequenceInputStream extends InputStream Constructor Summary SequenceInputStream(Enumeration e) e is an Enumeration of objects whose runtime type is InputStream. SequenceInputStream(InputStream s1, InputStream s2) Method Summary : All overriding those from inputStream. int available(), void close(), int read(), int read(byte[] b [, int off, int len])

48 Working with Filtered Streams
You attach a filtered stream to another stream to filter the data as it's read from or written to the original stream. Subclasses of either FilterInputStream or FilterOutputStream: DataInputStream and DataOutputStream : primitive data  bytes BufferedInputStream and BufferedOutputStream : LineNumberInputStream : (deprecated) PushbackInputStream : PrintStream : Subclasses of FilterReader and FilterWriter: BushbackReader, LineNumberReader, … Note: FilterReader/Writer use a character stream as its underlying stream while FilterInputStream/OutputStream use a byte stream as its underlying stream.

49 Writing Your Own Filtered Streams
Steps Create a subclass of FilterInputStream and FilterOutputStream. Override the read and write methods. Override and/or define any other methods that you might need. Make sure the input and output streams work together. Example: Use Adler32 to implement a CheckedInputStream and 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.

50 public class CheckedOutputStream extends FilterOutputStream {
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; }}

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

52 public interface Checksum { public void update(int b);
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;

53 public void update(int b) {
int s1 = value & 0xffff; int s2 = (value >> 16) & 0xffff; s1 += b & 0xff; s2 += s1; value = ((s2 % BASE) << 16) | (s1 % BASE); } public void update(byte[] b, int off, int len) { 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; } }

54 public class CheckedIOTest {
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(); }}

55 The java.io.DataInputStream class
public class DataInputStream extends FilterInputStream implements DataInput // allow to read primitive data in machine indep. way Constructor Summary DataInputStream(InputStream in) Method Summary int read(byte[] b [, int offset , int length ] ) // read to array b void readFully(byte[] [int,int]) // throws EOFException if not fully read <type> read<Type>( ) // read one data item <type> is one of boolean, byte, short, int, long, float, double, 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

56 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.

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

58 // 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(); }}

59 How read/writeUFT8() work
Note: There is no writeUnsignedByte() or writeUnsignedShort() in DataOutputStream. writeByte(-128) => readByte() = -128 and readUnsignedByte() = 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: instead of when writeUTF(String) the first 2 bytes are an unsigned short indicating the number of bytes required to write the string.

60 Have learned how to read/write data from a file,
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).

61 Path Name vs Abstract path name
Pathname [strings] are system-dependant strings for identifying directories or files in a system, while abstract pathnames are system independent strings for identifying directories or files. pathname = sys-dep-prefix ( DirName FileSpearator)* [ DirName | FileName ] FileSeparator is ‘/’ for UNIX and ‘\’ for WIN32. Default determined by “file.separator” system property; given at field File.separator abstract pathname = [ sys-dep-prefix ] DirName * [ DirName | FileName ] where sys-dep-prefix is an optional system-dependent prefix string, Unix: => “/” for UNIX root directory. win32: => “Driver:”, “Driver:\" , or "\\" for a Win32 UNC(universal Naming Convention)pathname, and A sequence of zero or more string names. Each name in an abstract pathname except for the last denotes a directory; the last name may denote either a directory or a file.

62 relative vs absolute pathnames
Pathnames and abstract pathname can be either absolute or relative. An absolute pathname contains full information to locate the file or directory that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other base directory pathname. absolute_pathname = base_directory relative_pathname where by default base_directory is the current user directory, which can be got by the system property “user.dir”.

63 public class File extends Object implements Serializable, Comparable
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.

64 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

65 static String pathSeparator
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.

66 static File createTempFile(String prefix, String suffix [, File dir])
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:\

67 // File properties getter/setter
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.

68 pathname/ File transformations
File getAbsoluteFile() ; String getAbsolutePath() ; Returns the absolute pathname string of this abstract pathname. File getCanonicalFile() ; String getCanonicalPath() ; Returns the canonical form of this abstract pathname. new File(“/a/b/../”).getCanonicalPath() // return C:\a but C:\a\b\.. for getAbsolutePth() String getParent() ; File getParentFile() Returns the pathname string or abstract pathname of this abstract pathname's 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

69 Directory and 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.

70 FilenameFilter and FileFilter
public interface FilenameFilter {public boolean accept(File dir, String name); } public interface FileFilter { public boolean accept(File dir); } Implementation of listFiles(FilenameFilter) public File[] listFiles( FilenameFilter filter) { String ss[] = list(); if (ss == null) return null; ArrayList v = new ArrayList(); for (int i = 0 ; i < ss.length ; i++) if (( filter == null) || filter.accept(this, ss[i]) ) v.add(new File( this.getpath() , ss[i])); return (File[])(v.toArray(new File[0])); }

71 File userDir = new File (System.getProperty(“user.dir”));
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]);

72 when to use 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.

73 How to Write to an ObjectOutputStream ? straight-forward! Ex:
Serializing Objects How to Write to an ObjectOutputStream ? straight-forward! Ex: FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeInt( ); s.writeObject("Today"); s.writeObject(new Date()); s.flush();

74 Notes about ObjectOutputStreams
If an object refers to other objects, then all of the objects that are reachable from the first must be written at the same time ObjectOutputStream stream implements the DataOutput interface that defines many methods for writing primitive data types, such as writeInt, writeFloat, or writeUTF. You can use these methods to write primitive data types to an 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. Serializable is a marker interface: you just declare you implement it but do not need to implement any additional method.

75 How to Read from an ObjectInputStream
EX: FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); int d = s.readInt(); 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. The order and types of objects read in must be the same as they were written out using ObjectOutputStream.

76 The java.io.datainput interface
public abstract interface java.io.DataInput { // Methods public abstract <type> read<Type>(); // <type> is any of 8 primitive types : byte, short, char, int, long, float, dobule, boolean. 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);}

77 The java.io.ObjectInput Interface
public abstract interface java.io.ObjectInput extends java.io.DataInput { // Methods public abstract int available(); public abstract void close(); public abstract int read(); public abstract int read(byte[] b); public abstract int read(byte[] b, int off, int len); public abstract Object readObject(); public abstract long skip(long n); }

78 java.io.ObjectInputStream
public class java.io.ObjectInputStream extends java.io.InputStream implements java.io.ObjectInput { // Constructors public ObjectInputStream(InputStream in); // Public Instance Methods; implementation of ObjectInput readObject(), readInt() ,… // 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

79 // Protected Instance Methods
protected final boolean enableResolveObject(boolean enable); protected void readStreamHeader(); protected Class resolveClass(ObjectInputStreamClass v); protected Object resolveObject(Object obj); }

80 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); }

81 public abstract void close(); public abstract void flush();
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); }

82 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();}

83 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

84 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 from read x and y ? Customizing serialization!!

85 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 ); }

86 public class LabeledPoint implements Serializable {
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!

87 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 Object<I/O>Stream will use either method if they can find them in target classes.

88 public class Employee implements Serializable, Externalizable {
Example public class Employee implements Serializable, Externalizable { String name; double salary; transiant Date hireDay ; public void writeExternal(ObjectOutput s ) { s.writeUTF(name); s.writeDouble(salary); 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);

89 References for Java Object Serialization
Java Object Serialization Specification ObjectInputStream ObjectOutputStream Serialize objects not serializable Serialization and magic methods

90 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.

91 public class java.io.RandomAccessFile extends java.lang.Object
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); }


Download ppt "Lecture 6. Java I/O Cheng-Chia Chen."

Similar presentations


Ads by Google