標籤:
8. 字元流Writer/Reader
Java中字元是採用Unicode標準,一個字元是16位,即一個字元使用兩個位元組來表示。為此,JAVA中引入了處理字元的流。
1. Reader抽象類別
用於讀取字元流的抽象類別。子類必須實現的方法只有 read(char[], int, int) 和 close()。但是,多數子類將重寫此處定義的一些方法,以提供更高的效率和/或其他功能。
1) FileReader :與FileInputStream對應
主要用來讀取字元檔案,使用預設的字元編碼,有三種建構函式:
(1)將檔案名稱作為字串 :FileReader f=new FileReader(“c:/temp.txt”);
(2)建構函式將File對象作為其參數。
File f=new file(“c:/temp.txt”);
FileReader f1=new FileReader(f);
(3) 建構函式將FileDescriptor對象作為參數
FileDescriptor() fd=new FileDescriptor()
FileReader f2=new FileReader(fd);
(1) 用指定字元數組作為參數:CharArrayReader(char[])
(2) 將字元數組作為輸入資料流:CharArrayReader(char[], int, int)
讀取字串,建構函式如下: public StringReader(String s);
2) CharArrayReader:與ByteArrayInputStream對應
3) StringReader : 與StringBufferInputStream對應
4) InputStreamReader
從輸入資料流讀取位元組,在將它們轉換成字元:Public inputstreamReader(inputstream is);
5) FilterReader: 允許過濾字元流
protected filterReader(Reader r);
6) BufferReader :接受Reader對象作為參數,並對其添加字元緩衝器,使用readline()方法可以讀取一行。
Public BufferReader(Reader r);
主要方法:
(1) public int read() throws IOException; //讀取一個字元,傳回值為讀取的字元
(2) public int read(char cbuf[]) throws IOException; /*讀取一系列字元到數組cbuf[]中,傳回值為實際讀取的字元的數量*/
(3) public abstract int read(char cbuf[],int off,int len) throws IOException;
/*讀取len個字元,從數組cbuf[]的下標off處開始存放,傳回值為實際讀取的字元數量,該方法必須由子類實現*/
2. Writer抽象類別
寫入字元流的抽象類別。子類必須實現的方法僅有 write(char[], int, int)、flush() 和 close()。但是,多數子類將重寫此處定義的一些方法,以提供更高的效率和/或其他功能。 其子類如下:
1) FileWrite: 與FileOutputStream對應
將字元類型資料寫入檔案,使用預設字元編碼和緩衝器大小。
Public FileWrite(file f);
2) chararrayWrite:與ByteArrayOutputStream對應 ,將字元緩衝器用作輸出。
Public CharArrayWrite();
3) PrintWrite:產生格式化輸出
public PrintWriter(outputstream os);
4) filterWriter:用於寫入過濾字元流
protected FilterWriter(Writer w);
5) PipedWriter:與PipedOutputStream對應
6) StringWriter:無與之對應的以位元組為導向的stream
主要方法:
(1) public void write(int c) throws IOException; //將整型值c的低16位寫入輸出資料流
(2) public void write(char cbuf[]) throws IOException; //將字元數組cbuf[]寫入輸出資料流
(3) public abstract void write(char cbuf[],int off,int len) throws IOException; //將字元數組cbuf[]中的從索引為off的位置處開始的len個字元寫入輸出資料流
(4) public void write(String str) throws IOException; //將字串str中的字元寫入輸出資料流
(5) public void write(String str,int off,int len) throws IOException; //將字串str 中從索引off開始處的len個字元寫入輸出資料流
(6) flush( ) //刷空輸出資料流,並輸出所有被緩衝的位元組。
(7)close() 關閉流 public abstract void close() throws IOException
3 .InputStream與Reader差別 OutputStream與Writer差別
InputStream和OutputStream類處理的是位元組流,資料流中的最小單位是位元組(8個bit)
Reader與Writer處理的是字元流,在處理字元流時涉及了字元編碼的轉換問題
import java.io.*; public class EncodeTest { private static void readBuff(byte [] buff) throws IOException { ByteArrayInputStream in =new ByteArrayInputStream(buff); int data; while((data=in.read())!=-1) System.out.print(data+" "); System.out.println(); in.close(); } public static void main(String args[]) throws IOException { System.out.println("記憶體中採用unicode字元編碼:" ); char c=‘好‘; int lowBit=c&0xFF; int highBit=(c&0xFF00)>>8; System.out.println(""+lowBit+" "+highBit); String s="好"; System.out.println("本地作業系統預設字元編碼:"); readBuff(s.getBytes()); System.out.println("採用GBK字元編碼:"); readBuff(s.getBytes("GBK")); System.out.println("採用UTF-8字元編碼:"); readBuff(s.getBytes("UTF-8")); } }
Reader類能夠將輸入資料流中採用其他編碼類別型的字元轉換為Unicode字元,然後在記憶體中為其分配記憶體
Writer類能夠將記憶體中的Unicode字元轉換為其他編碼類別型的字元,再寫到輸出資料流中。
9. IOException異常類的子類
1.public class EOFException :
非正常到達檔案尾或輸入資料流尾時,拋出這種類型的異常。
2.public class FileNotFoundException:
當檔案找不到時,拋出的異常。
3.public class InterruptedIOException:
當I/O操作被中斷時,拋出這種類型的異常。
Java輸入輸出資料流 3