Title: Stream
115
Stream
2Stream
- ?? ?? ????? ??? ?? ?? ???
- ??? ??? ??? ??.
- Stream ? Input Stream ? Output Stream?? ?? ? ??.
- Stream ? Byte Stream ? Character Stream?? ?? ?
??. - Byte Stream ???,??? ??,??,???
- Character Stream ??,?? ??,?? ?
Input Stream
?? ?? ????
?? Stream
Output Stream
3NodeStream, FilterStream
- node Stream Class
- Stream???? ???? ?? ??? ? ? ?? Class
- InputStream,OutputStream,FileinputStream,FileOutpu
tStream - filter Stream Class
- node Stream Class? ??? ?? ??.
- ? ?? ?? ??.
- DataInputStream,DataOutputStream
?? ?? ????
?? Stream
node Stream
filter Stream
4Byte Stream ?,??
- InputStream Class? OutputStream Class? ? ??
Class? ??? ?? ??. - 8?? ??? ????? ?? ??.
- ??? ? ??? ?? ??? ??
InputStream
?? ?? ????
?? Stream
OutputStream
5Character Stream ?,??
- Reader Class? Writer Class? ? ?? Class? ??? ??
??. - 16?? ??? ???? ?? ?? ????? ??
- ??? ?? ??? ?? ??? ??(?????)
Reader
?? ?? ????
?? Stream
Writer
6??? ???? ?? ???? ??
- InputStreamReader Class
- ??? ?? ????? ???? ??? ?? ?? ????? ?? ??? ??? ??.
- OutputStreamWriter Class
- ?? ?? ???? ??? ???? ???? ??? ? ? ??? ? ??.
- ???? ???? ?? ??? ?? ??
7?? ???
???/??? ??? ??? ?????
??? ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
??? StringBufferInputStream StringReader StringWriter
??? PipedInputSteam PipedOutputSteam PipedReader PipedWriter
?? FileInputStream FileOutputStream FileReader FileWriter
8?? ???
?? ??? ??? ?????
??? BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
??? FilterInputStream FilterOutputStream FilterReader FilterWriter
???-gt?? InputStreamReader InputStreamWriter
??? ?? SequenceInputStream
????? ObjectInputStream ObjectOutputStream
???? DataInputStream DataOutputStream FileReader FileWriter
??? PrintStream PrintWriter
9???( Stream ) Object
FileOutputStream
?? ??
ByteArrayOutputStream
OutputStream
??? ??? ??
PipedOutputStream
PipedInputStream? ??
FilterOutputStream
??? ?? ??? Class
10InputStream , OutputStream
import java.io. public class
Ex001InputStream public static void main(
String arg ) int ch InputStream in
System.in OutputStream out System.out
11InputStream , OutputStream
try while((ch in.read()) !
-1) System.out.print((char)ch)
//out.write(ch) //out.flush() //out.clos
e() catch(IOException e)
12FileInputStream , FileOutputStream
- ??? ?? ??? ?,?? Class
- ??? ???? ?? ?? ( ?? ?? ?? )
import java.io. public class
Ex002FileInputStream public static void main(
String arg ) throws Exception int ch , len
0 FileInputStream in new
FileInputStream("Ex001InputStream.java") FileOu
tputStream out new FileOutputStream("Ex001Inp
utSteram.java.back") System.out.println("??
??")
13FileInputStream , FileOutputStream
try while((ch in.read()) !
-1) out.write(ch) len
catch(IOException e) in.close() out.
close() System.out.println("?? ? , ?? " len
" Byte " )
14DataInputStream , DataOutputStream
DataOutputStream
??? ??? ???? ? ?? ???? ???
FilterOutputStream
PrintStream
?? ??? ??
BufferedOutputStream
?? ???? ??? ?? ?? ??? ??
15DataInputStream , DataOutputStream
- ???? ?? ?? ?? ?? ? ? ?? ??? ???
- ?? ???? ?? ? ? ??.
import java.io. public class Ex003DataInputStrea
m public static void main( String arg )
throws Exception FileOutputStream fout
new FileOutputStream("Ex003DataInputStream.dat"
) DataOutputStream out new
DataOutputStream(fout) System.out.println("??
??") out.writeUTF("??") out.writeInt(20)
out.writeUTF("??") out.writeInt(20) out.c
lose()
16DataInputStream , DataOutputStream
FileInputStream fin new FileInputStream("Ex
003DataInputStream.dat") DataInputStream in
new DataInputStream(fin) System.out.println(
"?? ??") System.out.println("??"
in.readUTF()) System.out.println("??"
in.readInt()) System.out.println("??"
in.readUTF()) System.out.println("??"
in.readInt()) in.close()
17BufferedReader , BufferedWriter
- ??? ???
- ?? ?? ?? Byte ??? I/O??? ??? ??? I/O? ?? ??.
- 16?? Unicode ????.
- Reader Class , Writer Class? ?? Class ??.
18BufferedReader , BufferedWriter
Writer
StringWriter
PrintWriter
CharArrayWriter
BufferedWriter
PipedWriter
FilterWriter
OutputStreamWriter
FileWriter
19BufferedReader , BufferedWriter
import java.io. public class
Ex004BufferedReader public static void main(
String arg ) throws Exception String str
int len 0 FileInputStream fin
new FileInputStream("Ex001InputStream.java")
InputStreamReader sin new InputStreamReader(fi
n) BufferedReader in new BufferedReader(sin)
FileOutputStream fout new
FileOutputStream("Ex001InputSteram.java.back") O
utputStreamWriter sout new OutputStreamWriter(fo
ut) BufferedWriter out new BufferedWriter(sout
)
20BufferedReader , BufferedWriter
System.out.println("?? ??") try whi
le((str in.readLine()) ! null) str
str "\n" out.write(str , 0 ,
str.length()) len catch(IOExcep
tion e) in.close() out.close() System.
out.println("?? ? , ?? " len " Byte "
)
21RandomAccesFile
- ??? ???? ??? ??? ? ? ??.
- ???
- RandomAccessFile(String filename , String
openmode) - r ??(??? ??? ?? )
- rw ?? / ??(?? ??)
- long getFilePointer() - ?? file pointer? ??? ???.
- void seek(long pos) - ??? ??? file pointer? ??.
- long length() - file? size? ???.
22source1
RandomAccessFile
import java.io. import java.util. public
class Ex005RandomAccessFile public static void
main( String arg ) throws Exception String
str "" RandomAccessFile raf new
RandomAccessFile("Ex001InputSteram.java.back","rw"
) raf.seek((long)10) System.out.println
(raf.readLine())
23source2
RandomAccessFile
raf.seek(raf.length()) raf.writeUTF(( new
Date().toString()) ) raf.close()
24SequenceInputStream
- ?? ?? ?? ???? ?? ?? ??.
- ?? ??? ?? ???
25source1
SequenceInputStream
import java.io. import java.util. public
class Ex006SequenceInputStream public static
void main( String arg ) throws
Exception int ch byte arr1
(byte)'H' , (byte)'e', (byte)'l', (byte)'l',
(byte)'o' , (byte)'\n' ByteArrayInputStream
bai new ByteArrayInputStream(arr1) FileInputS
tream in new FileInputStream("Ex001InputStrea
m.java")
26source2
SequenceInputStream
Vector v new Vector() v.add(bai) v.add
(in) SequenceInputStream sin new
SequenceInputStream(v.elements()) while((ch
sin.read()) ! -1) System.out.write(ch)
in.close()
27PipedInputStream
- PipedOutputStream ? ?? ??? ??.
- ??? ??? ???? ?? ? ??
?? ?? ????
PipedOutputStream
?? ?? ????
PipedInputStream
28Source1
PipedInputStream
import java.io. class Ex007ThreadInput extends
Thread PipedInputStream pins public
Ex007ThreadInput() pins new
PipedInputStream() public void
setPouts(PipedOutputStream pouts) try
pins.connect(pouts) catch(Exception
e) System.out.println(e)
29Source2
PipedInputStream
public void run() int ch try
System.out.println("pins.run()") while
(true) while((ch pins.read()) !
-1) System.out.println((char)ch)
catch(IOException
e) System.out.println(e)
30Source3
PipedInputStream
class Ex007ThreadOutput extends
Thread PipedOutputStream pouts public
Ex007ThreadOutput() pouts new
PipedOutputStream() public
PipedOutputStream getPouts() return
pouts
31Source4
PipedInputStream
public void run() try pouts.wr
ite("Ex007ThreadOutput".getBytes()) System.ou
t.println("pouts.write()") catch(Exceptio
n e) System.out.println(e)
32Source5
PipedInputStream
public class Ex007PipedInputStream public
static void main( String arg ) throws
Exception Ex007PipedInputStream obj new
Ex007PipedInputStream() Ex007ThreadInput pin
new Ex007ThreadInput() Ex007ThreadOutput pout
new Ex007ThreadOutput() pin.setPouts(pout.
getPouts()) pin.start() pout.start()
Thread.sleep(2000) pin.stop() pout.stop
()
33FilterInputStream , FilterOutputStreamStream
- ?? ??? ?,?? ?????.
- ???? ?? ??? ??? ???? ?? ??? ??
- FilterReader , FilterWriter class
- ?? ?? ?,?? ???
34Stream Type? ??
Type /?? Byte type(8bit) Stream Character (16bit) Type Stream
Source Stream InputStream Reader
Sink Stream OutputStream Writer
35Input Stream chain exam
Data Source
program
FileInputStream
BufferedInputStream
DataInputStream
36OutputStream chain exam
Data Source
program
DataOutputStream
BufferedOutputStream
FileOutputStream
37InputStream methods
- int read()int read(byte buffer)int
read(byte buffer, int offset, int length)void
close()int availablevoid mark(int readlimit)
38OutputStream methods
- void write()
- void write(byte buffer)
- void write(byte buffer, int offset, int length)
- void close()
- void flush()
39Reader methods
- int read()
- int read(char cbuf)
- int read(char cbuf), int offset, int length)
- void close()
- Boolean ready()
- void mark(int readAheadLimit)
40Writer methods
- void write(int c)
- void write(char cbuf)
- void write(char cbuf, int offset, int length)
- void write(String string)
- void write(String string, int offset, int length)
- void close()
- void flush()
41Node Streams
?? Character Streams Byte Streams
File FileReader FileWriter FileInputStream FileOutPutStream
Memory Array CharArrayReader CharArrayWriter ByteArrayInputStream ByteArrayOutputStream
Memory String StringReader StringWriter
Pipe PipedReader PipedWriter PipedInputStream PipedOutputStream
42?? ???
?? ??? ??? ?????
??? BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
??? FilterInputStream FilterOutputStream FilterReader FilterWriter
???-gt?? InputStreamReader InputStreamWriter
??? ?? SequenceInputStream
????? ObjectInputStream ObjectOutputStream
???? DataInputStream DataOutputStream FileReader FileWriter
??? PrintStream PrintWriter
43Byte Stream classes
InputStream
OutputStream
44OutputStream Object
FileOutputStream
?? ??
ByteArrayOutputStream
??? ??? ??
OutputStream
PipedOutputStream
PipedInputStream? ??
FilterOutputStream
Abstract class
ObjectOutputStream
45FilterOutputStream Object
??Class? ??????? ????
PushbackInputStream
FilterOutputStream
BufferedOutputStream
PrintStream
46InputStream Object
FileInputStream
?? ??
ObjectInputStream
ByteArrayInputStream
InputStream
??? ??? ??
PipedInputStream
PipedOutputStream? ??
SequenceInputStream
FilterInputStream
??Class? ??????? ????
StringBufferedInputStream
47FilterInputStream Object
DataInputStream
??Class? ??????? ????
PushbackInputStream
FilterInputStream
BufferedInputStream
LineNumberInputStream