throws IOException

Download Report

Transcript throws IOException

Object-Oriented Design and Programming (Java)
Topics Covered Today
• 3.1 Input and Output Programming
– 3.1.0 Java I/O Stream
– 3.1.1 File I/O
– 3.1.2 Using File I/O in the Library System
2
Java I/O Stream
•
•
•
•
•
•
•
•
•
•
•
What is an I/O stream?
Types of Streams
Stream class hierarchy
Control flow of an I/O operation using Streams
Byte streams
Character streams
Buffered streams
Standard I/O streams
Data streams
Object streams
File class
3
What is an I/O Stream?
• An I/O Stream represents an input source or an
output destination
• A stream can represent many different kinds of
sources and destinations
– disk files, devices, other programs, a network socket,
and memory arrays
• Streams support many different kinds of data
– simple bytes, primitive data types, localized characters,
and objects
4
I/O Streams
• No matter how they work internally, all streams
present the same simple model to programs that
use them
– A stream is a sequence of data
5
Input Stream
• A program uses an input stream to read data from a source
(a file, memory, a socket), and reads the information
sequentially, one item at a time.
6
Output Stream
• A program uses an output stream to write data to a
destination, and writing the information out sequentially,
one item at time.
7
Types of Streams
• General Stream Types
– Character and Byte Streams
• Character vs. Byte
– Input and Output Streams
• Based on source or destination
– Node and Filter Streams
• Whether the data on a stream is manipulated or transformed
or not
8
Character and Byte Streams
• Byte streams
– For binary data
– Root classes for byte streams:
• The InputStream Class
• The OutputStream Class
• Both classes are abstract
– Classes of Byte Streams
•
•
•
•
•
•
•
InputStream、OutputStream
FileInputStream、FileOutputStream
PipedInputStream、PipedOutputStream
ByteArrayInputStream、ByteArrayOutputStream
FilterInputStream、FilterOutputStream
DataInputStream、DataOutputStream
BufferedInputStream、BufferedOutputStream
9
Character and Byte Streams
• Character Streams
– For Unicode characters
– Root classes for character streams
• The Reader class
• The Writer class
• Both classes are abstract
– Most programs should use readers and writers to read and write
textual information.
•
•
•
•
•
•
•
•
Reader、Writer
InputStreamReader、OutputStreamWriter
FileReader、FileWriter
CharArrayReader、CharArrayWriter
PipedReader、PipedWriter
FilterReader、FilterWriter
BufferedReader、BufferedWriter
StringReader、StringWriter
10
Input and Output Streams
• Input or source streams
– Can read from these streams
– Root classes of all input streams:
• The InputStream Class
• The Reader Class
• Output or sink (destination) streams
– Can write to these streams
– Root classes of all output streams:
• The OutputStream Class
• The Writer Class
11
Node and Filter Streams
• Node streams (Data sink stream)
– Contain the basic functionality of reading or writing
from a specific location
– Types of node streams include files, memory and pipes
• Filter streams (Processing stream)
– Layered onto node streams between threads or
processes
– For additional functionality- altering or managing data
in the stream
• Adding layers to a node stream is called stream
chaining (stream链)
12
java.io hierarchies
13
Abstract Classes
• InputStream & OutputStream
• Reader & Writer
14
InputStream Abstract Class
• Read data from InputStream:
– int read( ); Reads the next byte of data from the input stream
– int read( byte b[ ] ); Reads some number of bytes from the
input stream and stores them into the buffer array b.
– int read( byte b[ ], int off, int len ); Reads up to len bytes of
data from the input stream into an array of bytes.
– int available( ); Returns the number of bytes that can be read
• Close Stream:
– close( ); Closes this input stream and releases any system
resources associated with the stream.
15
InputStream Abstract Class
16
Node InputStream Classes
17
Filter InputStream Classes
18
OutputStream Abstract Class
• Write data:
– void write( int b ); Writes the specified byte to this output stream.
– void write( byte b[ ] ); Writes b.length bytes from the specified
byte array to this output stream
– void write( byte b[ ], int off, int len ); Writes len bytes from the
specified byte array starting at offset off to this output stream.
• Close stream:
– close( );
• Buffer flush:
– flush( ); Flushes this output stream and forces any buffered
output bytes to be written out.
19
Node OutputStream Classes
20
Filter OutputStream Classes
21
The Reader Class
• Read character(s)
– public int read() throws IOException;
– public int read(char cbuf[]) throws IOException;
– public abstract int read(char cbuf[],int off,int len)
throws IOException;
• Close reader
– public abstract void close() throws IOException;
22
The Reader Class
23
Node Reader Classes
24
Filter Reader Classes
25
The Writer Class
• Write character(s) to output stream
– public void write(int c) throws IOException;
– public void write(char cbuf[]) throws IOException;
– public abstract void write(char cbuf[],int off,int len) throws
IOException;
– public void write(String str) throws IOException;
– public void write(String str,int off,int len) throws IOException;
• Flush
– flush( )
• Close stream
– public abstract void close() throws IOException;
26
Node Writer Classes
27
Filter Writer Classes
28
Control Flow of an I/O operation
Reading
1.Create a stream object and
associate it with a data-source
2.Give the stream object the
desired functionality through
stream chaining
Writing
1.Create a stream object and
associate it with a data-destination
2.Give the stream object the
desired functionality through
stream chaining
3.while (there is more information) 3.while (there is more information)
4. read next data from the stream
4. write next data to the stream
5.close the stream
5. close the stream
29
Byte Stream
• Programs use byte streams to perform input and
output of 8-bit bytes
• All byte stream classes are descended from
InputStream and OutputStream
• There are many byte stream classes
– FileInputStream and FileOutputStream
30
When Not to Use Byte Streams?
• Byte Stream represents a kind of low-level I/O that
you should avoid
– If the data contains character data, the best approach is
to use character streams
– There are also streams for more complicated data types
• Byte streams should only be used for the most
primitive I/O
• All other streams are based on byte stream
31
Example: FileInputStream & FileOutputStream
32
Example: FileInputStream & FileOutputStream
33
Character Stream
• The Java platform stores character values using Unicode
conventions
• Character stream I/O automatically translates this internal format to
and from the local character set.
– In Western locales, the local character set is usually an 8-bit
superset of ASCII.
• All character stream classes are descended from Reader and Writer
• As with byte streams, there are character stream classes that
specialize in file I/O: FileReader and FileWriter.
34
Example: FileReader & FileWriter
35
Example: FileReader & FileWriter
36
InputStreamReader & OutputStreamWriter
• public InputStreamReader(InputStream in);
– processing stream
– reads bytes from an InputStream and converts them to characte
• public InputStreamReader(InputStream in,String enc) throws
UnsupportedEncodingException;
– charset decoder
– ISO8859-1,UTF-8,UTF-16
• public OutputStreamWriter(OutputStream out);
– Processing stream
– converts characters to bytes
• public OutputStreamWriter(OutputStream out,String enc) throws
UnsupportedEncodingException;
37
Line-oriented I/O
38
Buffered Stream
• Why Buffered Streams?
– An unbuffered I/O means each read or write request is handled
directly by the underlying OS
• This can make a program much less efficient, since each such request
often triggers disk access, network activity, or some other operation that
is relatively expensive.
– To reduce this kind of overhead, the Java platform implements
buffered I/O streams
• Buffered input streams read data from a memory area known as a buffer;
the native input API is called only when the buffer is empty
• Similarly, buffered output streams write data to a buffer, and the native
output API is called only when the buffer is full.
39
How to create Buffered Streams?
• A program can convert a unbuffered stream into a
buffered stream using the wrapping idiom
– A unbuffered stream object is passed to the constructor
for a buffered stream class
– Example
inputStream = new BufferedReader(
new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(
new FileWriter("characteroutput.txt"));
40
Buffered Stream Classes
• BufferedInputStream and BufferedOutputStream
create buffered byte streams
• BufferedReader and BufferedWriter create
buffered character streams
41
Flushing Buffered Streams
• It often makes sense to write out a buffer at critical points,
without waiting for it to fill. This is known as flushing the
buffer.
• Some buffered output classes support autoflush, specified
by an optional constructor argument.
– When autoflush is enabled, certain key events cause the buffer
to be flushed
– For example, an autoflush PrintWriter object flushes the buffer
on every invocation of println or format.
• To flush a stream manually, invoke its flush method
– The flush method is valid on any output stream, but has no
effect unless the stream is buffered.
42
BufferedReader & BufferedWriter
• Create buffered stream
–
–
–
–
public BufferedReader(Reader in); use the default buffer size
public BufferedReader(Reader in, int sz);
public BufferedWriter(Writer out);
public BufferedWriter(Writer out, int sz);
• BufferedReader
– public String readLine() throws IOException; Read a line of text
• BufferedWriter
– public void newLine() throws IOException; Write a line
separator.
43
Standard Streams on Java Platform
• Three standard streams
– Standard Input, accessed through System.in
– Standard Output, accessed through System.out
– Standard Error, accessed through System.err
• These objects are defined automatically and do not need to be
opened
• System.out and System.err are defined as PrintStream objects
44
Data Streams
• Data streams support binary I/O of primitive data type
values (boolean, char, byte, short, int, long, float, and
double) as well as String values
• All data streams implement either the DataInput interface
or the DataOutput interface
• DataInputStream and DataOutputStream are most widelyused implementations of these interfaces
45
DataOutputStream
• DataOutputStream can only be created as a
wrapper for an existing byte stream object
46
DataInputStream
• Like DataOutputStream, DataInputStream must be constructed as a
wrapper for a byte stream
• End-of-file condition is detected by catching EOFException,
instead of testing for an invalid return value
47
Object Streams
• Object streams support I/O of objects
– Like Data streams support I/O of primitive data types
– The object has to be Serializable type
• The object stream classes are ObjectInputStream
and ObjectOutputStream
– These classes implement ObjectInput and
ObjectOutput, which are subinterfaces of DataInput
and DataOutput
– An object stream can contain a mixture of primitive
and object values
48
Input and Output of Complex Object
• The writeObject and readObject methods are simple to
use, but they contain some very sophisticated object
management logic
– This isn't important for a class like Calendar, which just
encapsulates primitive values. But many object contain
references to other objects.
• If readObject is to reconstitute an object from a stream, it
has to be able to reconstitute all of the objects the original
object referred to.
– These additional objects might have their own references, and so
on.
49
WriteObject
• The writeObject traverses the entire web of object
references and writes all objects in that web onto
the stream
– A single invocation of writeObject can cause a large
number of objects to be written to the stream.
50
I/O of multiple referred-to objects
• Object a contains references to objects b and c, while b contains
references to d and e
51
Close Streams
• Always Close Streams
– Closing a stream when it's no longer needed is very
important — so important that your program should
use a finally block to guarantee that both streams will
be closed even if an error occurs
– This practice helps avoid serious resource leaks.
52
Example: Closing File
BufferedReader fileIn = new
BufferedReader(new
FileReader(filename));
PrintWriter fileOut = new PrintWriter(new
FileWriter(filename));
...
fileIn.close();
fileOut.close();
53
Topics Covered Today
• 3.1 Input and Output Programming
– 3.1.0 Java I/O Stream
– 3.1.1 File I/O
– 3.1.2 Using File I/O in the Library System
54
The java.io.File Class
• Either a directory or a file
• Constructor:
– File(String pathname)
– File(File parent, String child)
– File(String parent, String child)
• File does not read and write, streams will handle
the read or write from/to a file.
55
Create java.io.File Class
• File(String pathname)
File f1 = new File("d:/ssd3/unit 3/DirList/.");
File f2 = new File(".");
• File(File parent, String child)
• File(String parent, String child)
File Dir = new File(“d:\\ssd3");
File FullFilename = new File(Dir, "FileDemo.raw ");
56
Listing Files
import java.io.*;
import java.util.*;
public class DirList {
public static void main(String[] args) {
File path = new File(".");
String[] list;
System.out.println(path.getAbsolutePath());
list = path.list();
for (int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
}
57
Other File Methods
•
•
•
•
•
•
•
•
canRead()
canWrite()
exists()
getParent()
isDirectory()
isFile()
lastModified()
length()
58
File Methods for Modifying
•
•
•
•
•
•
•
createNewFile()
delete()
makeDir()
makeDirs()
renameTo()
setLastModified()
setReadOnly()
59
FileInputStream
• Create a FileInputStream
– public FileInputStream(String name)
– public FileInputStream(File file)
• FileNotFoundException is thrown if
– the named file does not exist,
– it is a directory rather than a regular file,
– for some other reason cannot be opened for reading
• Closes this file input stream and releases any system
resources associated with the stream
60
FileInputStream Example
import java.io.*;
class MainClass {
public static void main(String args[])throws Exception{
// 1. Create File object
FileInputStream inFile=new FileInputStream("c:\\test.txt");
// 2. Create Reader Object to read the text
BufferedReader reader=new BufferedReader(
new InputStreamReader(inFile));
// 3. Read data
String strLine;
while((strLine=reader.readLine())!=null)
System.out.println(strLine);
reader.close();
inFile.close();
}
}
61
FileOutputStream
• Create File Object
java.io.File file = new java.io.File("C:\\test.txt");
• Create FileOutputStream
java.io.OutputStream os = new java.io.FileOutputStream(file);
• Write data
– os.write(byte [ ] b)
62
FileOutputStream Example
byte[] Data = new byte[100];
java.io.File file = new java.io.File("C:\\test.txt ");
try{
java.io.OutputStream os = new FileOutputStream(file);
try {
os.write(Data);
} catch(java.io.IOException e) { }
} catch (java.io.FileNotFound e) { }
63
FileReader
• FileReader(File file)
• FileReader(String fileName)
// 1. Create Reader Object to read the text
BufferedReader reader = new BufferedReader(
new FileReader("c:\\test2.txt"));
// 2. Read data line by line
String strLine;
while((strLine = reader.readLine()) != null)
System.out.println(strLine);
reader.close();
64
FileWriter
• Again, basically the same. The constructors are
– FileWriter(File file)
– FileWriter(String fileName)
– FileWriter(String fileName, boolean append)
• The last one allows appending, rather than writing
to the beginning (and erasing an existing file!).
• These will create files!
65
File I/O Example
• Program description:
– It first prompts the user for two file names, the name of
an input file and the name of an output file, and then
copies the contents of the input file to the output file.
66
CopyFile.java
import java.io.*;
public class CopyFile {
private static BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter( System.out, true);
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
67
CopyFile.java
public static void main(String[] args) throws IOException {
stdErr.print("Source filename: ");
stdErr.flush();
BufferedReader input = new BufferedReader(
new FileReader(stdIn.readLine()));
stdErr.print("Destination filename: ");
stdErr.flush();
PrintWriter output = new PrintWriter(new FileWriter(stdIn.readLine()));
String line = input.readLine();
while (line != null) {
output.println(line);
line = input.readLine();
}
input.close();
output.close();
stdOut.println("done");
}}
68
Random-Access file
• RandomAccessFile allow you to read/write data
beginning at the a specified location
– a file pointer is used to guide the starting position
– it supports both input and output with both bytes and characters
• Methods of Interface DataInput:
– readBoolean( )
– readInt( )
– readLine( )
• Method of Interface DataOutput:
– writeChar( )
– writeDouble( )
69
Using a Random Access File
import java.io.*;
public class RAFDemo {
public static void main(String[] args) {
try {
File f = new File(“filename");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
// Read a character
char ch = raf.readChar();
// Seek to end of file
raf.seek(f.length());
// Append to the end
raf.writeChars("aString");
raf.close();
} catch (IOException e) { }
}
}
70
File Stream
• The file streams read or write from a file on the
native file system
• File Streams
–
–
–
–
FileReader
FileWriter
FileInputStream
FileOutputStream
71
Topics Covered Today
• 3.1 Input and Output Programming
– 3.1.0 Java I/O Stream
– 3.1.1 File I/O
– 3.1.2 Using File I/O in the Library System
72
Add More Features
• The data for the library catalog will be stored in a
data file and the library system will load the
catalog from that file.
– catalog.dat
• The data formats are:
– Book_code_title_year_author_numberOfPages
– Recording_code_title_year_performer_format
• Borrower data will be write to a file in one of three
formats: Plain text, HTML and XML.
73
UML class diagram
74
Interface LibraryCatalogLoader
/**
* This interface declares a method for obtaining a library catalog
* from a file.
*/
public interface LibraryCatalogLoader {
/**
* Loads the information in the specified file into a library
* catalog and returns the catalog.
*
* @param filename the name of the file that contains the catalog data.
* @return a {@link Catalog}.
* @throws FileNotFoundException if the specified file does not exist.
* @throws IOException if there is an error reading the
*
information in the specified file.
* @throws DataFormatException if the file contains malformed data.
*/
Catalog loadCatalog(String filename) throws IOException,
FileNotFoundException,
DataFormatException;
}
75
Class FileLibraryCatalogLoader
import java.util.*;
import java.io.*;
/**
* Creates a library catalog and loads it with data stored in a file.
*
* @see Catalog
* @see CatalogItem
* @see Recording
* @see Book
*/
public class FileLibraryCatalogLoader implements LibraryCatalogLoader {
/* Prefix of a line with book data */
private final static String BOOK_PREFIX = "Book";
/* Prefix of a line with recording data */
private final static String RECORDING_PREFIX = "Recording";
/* Delimiter */
private final static String DELIM = "_";
76
Class FileLibraryCatalogLoader
/**
* Loads the information in the specified file into a library catalog and returns the catalog.
* @param filename The name of a file that contains catalog information.
* @return a library catalog.
* @throws FileNotFoundException if the specified file does not exist.
* @throws IOException if there is an error reading the information in the specified file.
* @throws DataFormatException if the file contains malformed data.
*/
public Catalog loadCatalog (String filename) throws IOException,
FileNotFoundException,
DataFormatException {
Catalog catalog = new Catalog();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while (line != null) {
CatalogItem item = null;
if (line.startsWith(BOOK_PREFIX)) { item = readBook(line); }
else if (line.startsWith(RECORDING_PREFIX)) { item = readRecording(line); }
else { throw new DataFormatException(line); }
catalog.addItem(item);
line = reader.readLine();
}
return catalog;
77
}
Class FileLibraryCatalogLoader
/**
* Extracts the book data in the specified line and returns
* a {@link Book} object that encapsulates the book data.
*
* @param line a string that contains book data.
* @return a <code>Book</code> object that encapsulates the
* book data in the specified line.
* @throws DataFormatException if the line contains errors.
*/
private Book readBook (String line) throws DataFormatException {
StringTokenizer tokenizer = new StringTokenizer(line, DELIM);
if (tokenizer.countTokens() != 6) { throw new DataFormatException(line); }
else {
try {
String prefix = tokenizer.nextToken();
return new Book (tokenizer.nextToken(), tokenizer.nextToken(),
Integer.parseInt(tokenizer.nextToken()),
tokenizer.nextToken(), Integer.parseInt(tokenizer.nextToken()));
} catch (NumberFormatException nfe) {
throw new DataFormatException(line);
}
}}
78
Class FileLibraryCatalogLoader
/**
* Extracts the recording data in the specified line and returns
* a {@link Recording} object that encapsulates the book data.
*
* @param line a string that contains recording data.
* @return a <code>Recording</code> object that encapsulates the
* recording data in the specified line.
* @throws DataFormatException if the line contains errors.
*/
private Recording readRecording (String line) throws DataFormatException {
StringTokenizer tokenizer = new StringTokenizer(line, DELIM);
if (tokenizer.countTokens() != 6) { throw new DataFormatException(line); }
else {
try {
String prefix = tokenizer.nextToken();
return new Recording (tokenizer.nextToken(), tokenizer.nextToken(),
Integer.parseInt( tokenizer.nextToken()),
tokenizer.nextToken(), tokenizer.nextToken());
} catch (NumberFormatException nfe) {
throw new DataFormatException(line);
}
79
Class DataFormatException
/**
* This exception is thrown when malformed data is detected while
* a file being parsed.
*/
public class DataFormatException extends Exception {
/**
* Constructs a <code>DataFormatException</code> with no detail message.
*/
public DataFormatException() { }
/**
* Constructs a <code>DataFormatException</code> with the
* specified detail message.
*
* @param message the malformed data
*/
public DataFormatException(String message) {
super(message);
}
}
80
Writing Data to a File
• LibrarySystem.java
81
Complete Library System
• The following files implement this version of the library
system:
–
–
–
–
–
–
–
–
–
–
–
Book.java
Recording.java
CatalogItem.java
Catalog.java
Borrower.java
BorrowedItems.java
BorrowerDatabase.java
BorrowersFormatter.java
PlainTextBorrowersFormatter.java
HTMLBorrowersFormatter.java
XMLBorrowersFormatter.java
82