Stream - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Stream

Description:

15 Stream InputStream methods int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length) void close() int ... – PowerPoint PPT presentation

Number of Views:132
Avg rating:3.0/5.0
Slides: 48
Provided by: ejavascho
Category:

less

Transcript and Presenter's Notes

Title: Stream


1
15
Stream
2
Stream
  • ?? ?? ????? ??? ?? ?? ???
  • ??? ??? ??? ??.
  • Stream ? Input Stream ? Output Stream?? ?? ? ??.
  • Stream ? Byte Stream ? Character Stream?? ?? ?
    ??.
  • Byte Stream ???,??? ??,??,???
  • Character Stream ??,?? ??,?? ?

Input Stream
?? ?? ????
?? Stream
Output Stream
3
NodeStream, FilterStream
  • node Stream Class
  • Stream???? ???? ?? ??? ? ? ?? Class
  • InputStream,OutputStream,FileinputStream,FileOutpu
    tStream
  • filter Stream Class
  • node Stream Class? ??? ?? ??.
  • ? ?? ?? ??.
  • DataInputStream,DataOutputStream

?? ?? ????
?? Stream
node Stream
filter Stream
4
Byte Stream ?,??
  • InputStream Class? OutputStream Class? ? ??
    Class? ??? ?? ??.
  • 8?? ??? ????? ?? ??.
  • ??? ? ??? ?? ??? ??

InputStream
?? ?? ????
?? Stream
OutputStream
5
Character 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
10
InputStream , OutputStream
import java.io. public class
Ex001InputStream public static void main(
String arg ) int ch InputStream in
System.in OutputStream out System.out
11
InputStream , OutputStream
try while((ch in.read()) !
-1) System.out.print((char)ch)
//out.write(ch) //out.flush() //out.clos
e() catch(IOException e)
12
FileInputStream , 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("??
??")
13
FileInputStream , FileOutputStream
try while((ch in.read()) !
-1) out.write(ch) len
catch(IOException e) in.close() out.
close() System.out.println("?? ? , ?? " len
" Byte " )
14
DataInputStream , DataOutputStream
DataOutputStream
??? ??? ???? ? ?? ???? ???
FilterOutputStream
PrintStream
?? ??? ??
BufferedOutputStream
?? ???? ??? ?? ?? ??? ??
15
DataInputStream , 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()
16
DataInputStream , 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()
17
BufferedReader , BufferedWriter
  • ??? ???
  • ?? ?? ?? Byte ??? I/O??? ??? ??? I/O? ?? ??.
  • 16?? Unicode ????.
  • Reader Class , Writer Class? ?? Class ??.

18
BufferedReader , BufferedWriter
Writer
StringWriter
PrintWriter
CharArrayWriter
BufferedWriter
PipedWriter
FilterWriter
OutputStreamWriter
FileWriter
19
BufferedReader , 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
)
20
BufferedReader , 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 "
)
21
RandomAccesFile
  • ??? ???? ??? ??? ? ? ??.
  • ???
  • RandomAccessFile(String filename , String
    openmode)
  • r ??(??? ??? ?? )
  • rw ?? / ??(?? ??)
  • long getFilePointer() - ?? file pointer? ??? ???.
  • void seek(long pos) - ??? ??? file pointer? ??.
  • long length() - file? size? ???.

22
source1
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())
23
source2
RandomAccessFile
raf.seek(raf.length()) raf.writeUTF(( new
Date().toString()) ) raf.close()
24
SequenceInputStream
  • ?? ?? ?? ???? ?? ?? ??.
  • ?? ??? ?? ???

25
source1
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")
26
source2
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()
27
PipedInputStream
  • PipedOutputStream ? ?? ??? ??.
  • ??? ??? ???? ?? ? ??

?? ?? ????
PipedOutputStream
?? ?? ????
PipedInputStream
28
Source1
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)
29
Source2
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)
30
Source3
PipedInputStream
class Ex007ThreadOutput extends
Thread PipedOutputStream pouts public
Ex007ThreadOutput() pouts new
PipedOutputStream() public
PipedOutputStream getPouts() return
pouts
31
Source4
PipedInputStream
public void run() try pouts.wr
ite("Ex007ThreadOutput".getBytes()) System.ou
t.println("pouts.write()") catch(Exceptio
n e) System.out.println(e)
32
Source5
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
()
33
FilterInputStream , FilterOutputStreamStream
  • ?? ??? ?,?? ?????.
  • ???? ?? ??? ??? ???? ?? ??? ??
  • FilterReader , FilterWriter class
  • ?? ?? ?,?? ???

34
Stream Type? ??
Type /?? Byte type(8bit) Stream Character (16bit) Type Stream
Source Stream InputStream Reader
Sink Stream OutputStream Writer
35
Input Stream chain exam
Data Source
program
FileInputStream
BufferedInputStream
DataInputStream
36
OutputStream chain exam
Data Source
program
DataOutputStream
BufferedOutputStream
FileOutputStream
37
InputStream methods
  • int read()int read(byte buffer)int
    read(byte buffer, int offset, int length)void
    close()int availablevoid mark(int readlimit)

38
OutputStream methods
  • void write()
  • void write(byte buffer)
  • void write(byte buffer, int offset, int length)
  • void close()
  • void flush()

39
Reader methods
  • int read()
  • int read(char cbuf)
  • int read(char cbuf), int offset, int length)
  • void close()
  • Boolean ready()
  • void mark(int readAheadLimit)

40
Writer 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()

41
Node 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
43
Byte Stream classes
InputStream
  • Byte Stream

OutputStream
44
OutputStream Object
FileOutputStream
?? ??
ByteArrayOutputStream
??? ??? ??
OutputStream
PipedOutputStream
PipedInputStream? ??
FilterOutputStream
Abstract class
ObjectOutputStream
45
FilterOutputStream Object
??Class? ??????? ????
PushbackInputStream
FilterOutputStream
BufferedOutputStream
PrintStream
46
InputStream Object
FileInputStream
?? ??
ObjectInputStream
ByteArrayInputStream
InputStream
??? ??? ??
PipedInputStream
PipedOutputStream? ??
SequenceInputStream
FilterInputStream
??Class? ??????? ????
StringBufferedInputStream
47
FilterInputStream Object
DataInputStream
??Class? ??????? ????
PushbackInputStream
FilterInputStream
BufferedInputStream
LineNumberInputStream
Write a Comment
User Comments (0)
About PowerShow.com