Download presentation
Presentation is loading. Please wait.
Published by谛 才 Modified over 7 years ago
1
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Keyboard class processing reading and writing text files object serialization
2
I/O Streams A stream is a sequence of bytes 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 A program can manage multiple streams at a time The java.io package contains many classes that allow us to define various streams with specific characteristics
3
I/O Stream Categories The classes in the I/O package divide input and output streams into other categories An I/O stream is either a character stream, which deals with text data byte stream, which deal with byte data An I/O stream is also either a data stream, which acts as either a source or destination processing stream, which alters or manages information in the stream
4
Character Streams - Unicode
5
Byte Streams
6
Character Streams VS. Byte Streams
A character stream manages Unicode characters, while a byte stream manages 8-bit bytes. All classes derived from Reader and Writer represent character streams, and all classes derived from InputStream and OutputStream represent byte streams.
7
Data Streams VS. Processing Streams
A data stream is a stream that represents a particular source or destination stream, such as a string in memory or a file on disk. A processing stream (sometimes called a filtering stream) performs some sort of manipulation on the data in a stream. By combining data streams with processing streams, we can create an input or output stream that behaves exactly as we wish. The classes that represent data streams and processing streams are the same classes that represent character streams and byte streams. It is just another way to categories them.
8
Source/Sink Streams VS. Filter Streams
9
The IOException Class Many operations performed by I/O classes can potentially throw an IOException. The IOException class is the parent of several exception classes that represent problems when trying to perform I/O. An IOException is a checked exception.
10
Standard I/O There are three standard I/O streams:
standard input – defined by System.in standard output – defined by System.out standard error – defined by System.err We use System.out when we execute println statements System.in is declared to be a generic InputStream reference, and therefore usually must be mapped to a more useful stream with specific characteristics
11
Chaining Streams FileInputStream fin = new FileInputStream(“employee.dat”);
12
Chaining Streams File f = new File(“employee.dat”) ;
FileInputStream fin = new FileInputStream( f ) ;
13
Chaining Streams File f = new File(“employee.dat”) ;
FileInputStream fin = new FileInputStream( f ) ; DataInputStream din = new DataInputStream ( fin ) ; double d = din.readDouble ( );
14
Chaining Streams DataInputStream din = new DataInputStream(
new FileInputStream( new File(“employee.dat”))) ; double d = din.readDouble ( ) ;
15
Chaining Streams FileInputStream fin = new FileInputStream("employee.dat"); BufferedInputStream bis = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bis); double d = din.readDouble();
16
Chaining Streams DataInputStream din = new DataInputStream( new BufferedInputStream( new FileInputStream("employee.dat"))); double d = din.readDouble();
17
Chaining Streams PushbackInputStream pbis = new PushbackInputStream( new BufferedInputStream( new FileInputStream("employee.dat"))); byte b = pbis.read(); //gets next byte if (b != '<') pbis.unread(b); //double d = din.readDouble(); ** can't **
18
Chaining Streams DataInputStream dis = new DataInputStream( pbis = new PushbackInputStream( new BufferedInputStream( new FileInputStream("employee.dat")))); byte b = pbis.read(); //gets next byte if (b != '<') pbis.unread(b); double d = din.readDouble(); ** can do this **
19
The Keyboard Class Now we can examine the processing of the Keyboard class in detail The Keyboard class: declares a useful standard input stream handles exceptions that may be thrown parses input lines into separate values converts input stings into the expected type handles conversion problems
20
The Standard Input Stream
The Keyboard class declares the following input stream: InputStreamReader isr = new InputStreamReader (System.in) BufferedReader stdin = new BufferedReader (isr); The InputStreamReader object converts the original byte stream into a character stream The BufferedReader object allows us to use the readLine method to get an entire line of input
21
Text Files Information can be read from and written to text files by declaring and using the correct I/O streams The FileReader class represents an input file containing character data See Inventory.java See InventoryItem.java The FileWriter class represents a text output file See TestData.java
22
Chaining Streams-Text
FileWriter fw = new FileWriter("somefile.txt"); //equivalent to: OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("somefile.txt"));
23
Chaining Streams-Text
PrintWriter pw = new PrintWriter( new FileWriter("somefile.txt")); pw.println("This will get written to the file."); // use is similar to System.out.println // println adds correct eol character for target // by System.getProperty("line.separator");
24
Chaining Streams-Text
PrintWriter pw = new PrintWriter( new FileOutputStream("somefile.txt")); //PrintWriter(OutputStream) automatically inserts an //OutputStreamWriter pw.println("This will get written to the file.");
25
Chaining Streams-Text
// no analogous PrintReader BufferedReader br = new BufferedReader( new FileReader("somefile.txt"); String s; while ((s != br.readLine()) != null){ //do something } // readLine() returns null when no more input //FileReader automatically converts bytes to Unicode
26
Chaining Streams-Text
// no automatic conversion like FileReader - must use //InputStreamReader BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); BufferedReader br2 = new BufferedReader( new InputStreamReader(url.openStream())); String s; while ((s != br.readLine()) != null){ //do something } // readLine() returns null when no more input
27
Object Serialization Object serialization is the act of saving an object, and its current state, so that it can be used again in another program The idea that an object can “live” beyond the program that created it is called persistence Object serialization is accomplished using the classes ObjectOutputStream and ObjectInputStream Serialization takes into account any other objects that are referenced by an object being serialized, saving them too
28
Object class public class DataObject implements Serializable{
String message; public DataObject(){ message = null; } public void setMessage(String m){ message = m; public String getMessage(){ return message;
29
Chaining Streams – Object Serialization
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("somefile.dat")); DataObject dataobject = new DataObject(); dataobject.setMessage("Hello"); oos.writeObject(dataobject); ObjectInputStream ois = new ObjectInputStream( new FileInputStream("somefile.dat"); DataObject inobject = (DataObject)ois.readObject();
30
String Tokenizer public void readData(BufferedReader in) throws IOException{ String s = in.readLine(); StringTokenizer t = new StringTokenizer(s, ","); String name = t.nextToken(); double salary = Double.parseDouble(t.nextToken()); int month = Integer.parseInt(t.nextToken()); . . . }
31
END Of Lecture 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.