JAVA IO OUTLINE IO streams Byte Streams Character

JAVA I/O

OUTLINE I/O streams Byte Streams Character Streams Buffered Stream I/O from the Command Line Standard I/O java. io. Console File I/O Data Streams Object Streams

I/O STREAMS 串流是一串的0和1所組成的連續串 從source端由input stream 讀入程式 read source 0101… program 由程式將output stream 寫入目的地 write program 0101… destina tion

BYTE STREAMS 位元組串流 Input和output一次讀入或寫入一個位元組(8 -bits) Ex. System. in, File. Input. Stream/File. Output. Stream

Java. io. * File. Input. Stream Filter. Input. Stream Buffered. Input. Stream File. Output. Stream Filter. Output. Stream Buffered. Output. Stream

SYSTEM. IN 一次讀入 8 bits Output時會印出ASCII code(A=65)

FILEINPUTSTREAM/FILEOUTPUTSTREA M File. Input. Stream從檔案讀入bytes File. Output. Stream 將bytes寫入目標檔案 當找不到檔案時會丟出例外 java. io. File. Not. Found. Exception

FILEINPUTSTREAM/FILEOUTPUTSTREA M

FILEINPUTSTREAM/FILEOUTPUTSTREA M Input. Stream t e s t o f r b y e t s t read() int c (b = 98) write(c) Output. Stream t e s t f o r b r e a m

CHARACTER STREAMS 字元串流 用萬國碼(Unicode)協定來處存字元值。處理中文字 時需要用character stream, 一次讀入 2 bytes Ex. File. Reader/File. Writer

Java. io. * Buffered. Reader Input. Stream. Reader File. Reader Buffered. Writer Output. Stream. Writer File. Writer

FILEREADER/FILEWRITER

FILEREADER/FILEWRITER File. Reader是character stream,一次讀取兩個字元,若 使用File. Input. Stream(是byte stream)則會將一個中文字 拆成兩個字元讀取


BUFFERED STREAM Buffered. Input. Stream, Buffered. Output. Stream是byte stream形式的暫存 Buffered. Reader, Buffered. Writer則是character stream形 式的暫存

BUFFERED STREAM

I/O FROM THE COMMAND LINE 由鍵盤讀取資料, 可供使用者輸入 read 0101… program

STANDARD I/O Standard input: System. in Standard output: System. out Standard error: System. err

STANDARD I/O 優化標準輸入 Buffered. Reader bf = new Buffered. Reader(new Input. Stream. Reader(System. in)); new Input. Stream. Reader(System. in)先將System. in標準 輸入由位元串列轉換成字元串列 再用buffered stream處理。放入buffer後,要使用時可 以一次讀取一整行轉成字串 bf. read. Line(),方便使用

STANDARD I/O





DATAINPUTSTREAM/DATAOUTPUTSTR EAM Data. Input. Stream reads bytes from the stream and converts them into appropriate primitive type values or strings. Data. Output. Stream converts primitive type values or strings into bytes and outputs the bytes to the stream.

DATAINPUTSTREAM/DATAOUTPUTSTR EAM Data. Input. Stream Data. Output. Stream read. Boolean() read. Byte() read. Char() read. Float() read. Int() … write. Boolean() write. Byte() write. Char() write. Float() write. Int() …

DATAINPUTSTREAM/DATAOUTPUTSTR EAM


OBJECTSTREAM
- Slides: 29