Java輸入輸出資料流(3),java輸入輸出資料流
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 輸入輸出資料流 (被採納為答案者加100分)
Input和Output
1. stream代表的是任何有能力產出資料的資料來源,或是任何有能力接收資料的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括兩種類型:
1.1 以位元組為導向的stream
以位元組為導向的stream,表示以位元組為單位從stream中讀取或往stream中寫入資訊。以位元組為導向的stream包括下面幾種類型:
1) input stream:
1) ByteArrayInputStream:把記憶體中的一個緩衝區作為InputStream使用
2) StringBufferInputStream:把一個String對象作為InputStream
3) FileInputStream:把一個檔案作為InputStream,實現對檔案的讀取操作
4) PipedInputStream:實現了pipe的概念,主要線上程中使用
5) SequenceInputStream:把多個InputStream合并為一個InputStream
2) Out stream
1) ByteArrayOutputStream:把資訊存入記憶體中的一個緩衝區中
2) FileOutputStream:把資訊存入檔案中
3) PipedOutputStream:實現了pipe的概念,主要線上程中使用
4) SequenceOutputStream:把多個OutStream合并為一個OutStream
1.2 以Unicode字元為導向的stream
以Unicode字元為導向的stream,表示以Unicode字元為單位從stream中讀取或往stream中寫入資訊。以Unicode字元為導向的stream包括下面幾種類型:
1) Input Stream
1) CharArrayReader:與ByteArrayInputStream對應
2) StringReader:與StringBufferInputStream對應
3) FileReader:與FileInputStream對應
4) PipedReader:與PipedInputStream對應
2) Out Stream
1) CharArrayWrite:與ByteArrayOutputStream對應
2) StringWrite:無與之對應的以位元組為導向的stream
3) FileWrite:與FileOutputStream對應
4) PipedWrite:與PipedOutputStream對應
以字元為導向的stream基本上對有與之相對應的以位元組為導向的stream。兩個對應類實現的功能相同,字是在操作時的導向不同。
如CharArrayReader:和ByteArrayInputStream的作用都是把記憶體中的一個緩衝區作為InputStream使用,所不同的是前者每次從記憶體中讀取一個位元組的資訊,而後者每次從記憶體中讀取一個字元。
1.3 兩種不現導向的stream之間的轉換
InputStreamReader和OutputStreamReader:把一個以位元組為導向的stream轉換成一個以字元為導向的stream。
2. stream添加屬性
2.1 “為stream添加屬性”的作用
運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。
如果我們要往一......餘下全文>>
java的輸入輸出資料流
package A;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyFileReader {
public static void main(String[] args) throws IOException {
File sf = new File("D:/IODemo1.txt");//注意檔案路徑正確,這裡測試放在D盤了
File df = new File("D:/IODemo2.txt");
MyFileReader.copy(sf, df);
}
public static void copy(File srcfile, File desfile) {
try {
InputStream is = new FileInputStream(srcfile);
OutputStream os = new FileOutputStream(desfile);
int c;
while ((c = is.read()) != -1) {
os.write(c);
}
is.close();
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("沒有找到檔案");
} catch (IOException e) {
e.printStackTrace();
System.out.println("檔案輸入輸出異常");
}
}
}