【文章內(nèi)容簡介】
: I n p u t S t r e am) Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 16 DataOutputStream DataOutputStream類擴(kuò)展 FilterOutputStream類并實(shí)現(xiàn) DataOutput接口。 j a va . i o . D a t a O u t p u t + w ri t eB o o l e a n ( b : Bo o l e a n ) : vo i d + w ri t eB yt e( v: i n t ) : vo i d + w ri t eB yt es( s: S t ri n g ) : v o i d + w ri t eC h a r( c: ch a r) : v o i d + w ri t eC h a rs ( s: S t r i n g ) : v o i d + w ri t eF l o a t ( v: f l o a t ) : v o i d + w ri t eD o u b l e( v: f l o a t ) : v o i d + w ri t eI n t ( v: i n t ) : v o i d + w ri t eL o n g ( v: l o n g ) : v o i d + w ri t e S h o rt( v: s h o r t ) : v o i d + w ri t eU T F( s: S t r i n g ) : v o i d 向輸出流寫入一個(gè)布爾值 向輸出流寫入?yún)?shù) v 的 8 個(gè)低階位 向輸出流寫入一個(gè)字符串的低字節(jié)字符 向輸出流寫入一個(gè)字符( 由 2 個(gè)字節(jié)構(gòu)成 ) 向輸出流寫入字符串 s 中的每個(gè)字符,依序兩個(gè)字節(jié)為一個(gè)字符 向輸出流寫入一個(gè) f l o at 值 向輸出流寫入一個(gè) d o u b l e 值 向輸出 流寫入一個(gè) i n t 值 向輸出流寫入一個(gè) l o n g 值 向輸出流寫入一個(gè) s h o r t 值 寫入一個(gè) U TF 格式的字符串 s O u t p u t S t re a m F i l t e r O u t p u t S t r e am D at aO u t p u t S t r e am + D at aO u t p u t S t r e am( o u t : O u t p u t S t r e am) Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 17 二進(jìn)制 I/O中的字符和字符串 一個(gè)統(tǒng)一碼由兩個(gè)字節(jié)構(gòu)成。方法 writeChar(char c)將字符 c的統(tǒng)一碼寫入輸出流。方法 writeChars(String s) 將字符串 s中的每個(gè)字符的統(tǒng)一碼都寫到輸出流中。 為什么使用 UTF8? 什么是 UTF8? UTF8是一種編碼方案,它允許系統(tǒng)和統(tǒng)一碼及 ASCII碼一起高效操作。大多數(shù)操作系統(tǒng)使用 ASCII碼, Java使用統(tǒng)一碼。ASCII碼字符集是統(tǒng)一碼字符集的子集。由于許多應(yīng)用程序只需要 ASCII碼字符集,所以將 8位的 ASCII碼轉(zhuǎn)化為 16位的統(tǒng)一碼是很浪費(fèi)的。 UTF8是一個(gè)可選的方案,它使用 1字節(jié)、 2字節(jié)或 3字節(jié)來存儲(chǔ)字符。 ASCII值(小于 0x7F)就使用一字節(jié)編碼。統(tǒng)一碼小于 0x7FF就使用兩字節(jié)如果字符編碼。其它統(tǒng)一碼值使用三個(gè)字節(jié)編碼。 Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 18 使用 DataInputStream/DataOutputStream 數(shù)據(jù)流用于對已經(jīng)存在的輸入 /輸出流進(jìn)行包裝,以便在原始流中過濾數(shù)據(jù)??梢允褂孟旅娴臉?gòu)造方法來創(chuàng)建它們: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream) 下面給出的語句會(huì)創(chuàng)建數(shù)據(jù)流。第一條語句為文件 輸入流;而第二條語句為文件 : DataInputStream infile = new DataInputStream(new FileInputStream())。 DataOutputStream outfile = new DataOutputStream(new FileOutputStream())。 TestDataStream Run Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 19 檢測文件末尾 提示:如果到達(dá) InputStream的末尾之后還繼續(xù)從中讀取數(shù)據(jù),就會(huì)發(fā)生 EOFException異常。如何檢測一個(gè)文件的末尾呢?可以使用 ()方法來檢測。當(dāng) () == 0 表明文件到達(dá)末尾。 順序和格式 警告:必須按存儲(chǔ)的順序和格式讀取文件中的數(shù)據(jù)。例如:學(xué)生的姓名是用 writeUTF方法以 UTF8格式寫入的,所以,讀取時(shí)必須使用 readUTF方法。 Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 20 BufferedInputStream/ BufferedOutputStream 使用緩沖來提高 輸入 /輸出的速度 I n p u t S t r e a m O u t p u t S t re a m O b j e ct O b j e ct O u t p u t S t r e am F i l t e r O u t p u t S t r e am F i l e O u t p u t S t r e am B u f f e r e d I n p u t S t r e am D at aI n p u t S t r e am B u f f e r e d O u t p u t S t r e am D at aO u t p u t S t r e am P r i n t S t r e am O b j e ct I n p u t S t r e am F i l t e r I n p u t S t r e am F i l eI n p u t S t r e am BufferedInputStream類和 BufferedOutputStream類都沒有包含新方法。 BufferedInputStream類和 BufferedOutputStream中的所有方法都是從InputStream類和 OutputStream類繼承而來的。 Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 21 構(gòu)造 BufferedInputStream流和BufferedOutputStream流 // Create a BufferedInputStream public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int bufferSize) // Create a BufferedOutputStream public BufferedOutputStream(OutputStream out) public BufferedOutputStream(OutputStreamr out, int bufferSize) Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 0132130807 22 實(shí)例學(xué)習(xí):復(fù)制文件 該實(shí)例開發(fā)一個(gè)復(fù)制文件的程序。用戶需要提供一個(gè)源文件與一個(gè)目標(biāo)文件作為命令行參數(shù),所使用的命令如下: java Copy source target 該程序?qū)⒃次募?fù)制到目標(biāo)文件,然后顯示這個(gè)文件中的字節(jié)數(shù)。如果源文件不存在,就會(huì)告知用戶找不到這個(gè)文件。如果目標(biāo)文件已經(jīng)存在,就告知用戶目標(biāo)文件存在。 Copy Run Liang, Introduction to Java Programming, Eighth Edition, (c) 2021 Pearson Education, Inc. All rights reserved. 01321308