Java的核心庫提供了全面的IO介面,包括:檔案讀寫,標準裝置輸出等等。Java中IO是以流為基礎進行輸入輸出的,所有資料被序列化寫入輸出流,或者從輸入流讀入。在具體使用中很多初學者對包的使用非常含糊,本文將詳細解說關於的使用。
1、Input和Output
am代表的是任何有能力產出資料的資料來源,或是任何有能力接收資料的接收源。在Java的IO系統中,所有的stream(包括Input和Out stream)都包括兩種型別:
1.1以位元組為導向的stream
以位元組為導向的stream,表示以位元組為單位從stream中讀取或往stream中寫入資訊。以位元組為導向的stream包括下面幾種型別:
input
stream:
1)ByteArrayInputStream:把記憶體中的一個緩衝區作為InputStream使用
2)StringBufferInputStream:把一個String物件作為InputStream
3)FileInputStream:把一個檔案作為InputStream,實現對檔案的讀取操作
4)PipedInputStream:實現了pipe的概念,主要線上程中使用
5)SequenceInputStream:把多個InputStream合併為一個InputStream
Out
stream
1)ByteArrayOutputStream:把資訊存入記憶體中的一個緩衝區中
2)FileOutputStream:把資訊存入檔案中
3)PipedOutputStream:實現了pipe的概念,主要線上程中使用
4)SequenceOutputStream:把多個OutStream合併為一個OutStream
1.2 以Unicode字元為導向的stream
以Unicode字元為導向的stream,表示以Unicode字元為單位從stream中讀取或往stream中寫入資訊。以Unicode字元為導向的stream包括下面幾種型別:
Input
Stream
1)CharArrayReader:與ByteArrayInputStream對應
2)StringReader:與StringBufferInputStream對應
3)FileReader:與FileInputStream對應
4)PipedReader:與PipedInputStream對應
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新增屬性。下面以一個例子來說明這種功能的作用。
如果我們要往一個檔案中寫入資料,我們可以這樣操作:
FileOutStream fs = new FileOutStream(“”);
然後就可以通過產生的fs物件呼叫write()函式來往檔案中寫入資料了。但是,如果我們想實現“先把要寫入檔案的資料先快取到記憶體 中,再把快取中的資料寫入檔案中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和 FilterOutStream的子類,為FileOutStream新增我們所需要的功能。
2.2FilterInputStream的'各種型別
2.2.1用於封裝以位元組為導向的InputStream
1)DataInputStream:從stream中讀取基本型別(int、char等)資料。
2)BufferedInputStream:使用緩衝區
3)LineNumberInputStream:會記錄input stream內的行數,然後可以呼叫getLineNumber()和setLineNumber(int)
4)PushbackInputStream:很少用到,一般用於編譯器開發
2.2.2用於封裝以字元為導向的InputStream
1)沒有與DataInputStream對應的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream
2)BufferedReader:與BufferedInputStream對應
3)LineNumberReader:與LineNumberInputStream對應
4)PushBackReader:與PushbackInputStream對應
2.3FilterOutStream的各種型別
2.2.3用於封裝以位元組為導向的OutputStream
1)DataIOutStream:往stream中輸出基本型別(int、char等)資料。
2)BufferedOutStream:使用緩衝區
3)PrintStream:產生格式化輸出
2.2.4用於封裝以字元為導向的OutputStream
1)BufferedWrite:與對應
2)PrintWrite:與對應
omAccessFile
1)可通過RandomAccessFile物件完成對檔案的讀寫操作
2)在產生一個物件時,可指明要開啟的檔案的性質:r,只讀;w,只寫;rw可讀寫
3)可以直接跳到檔案中指定的位置
4.I/O應用的一個例子
java程式碼
import .*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行為單位從一個檔案讀取資料
BufferedReader in = new BufferedReader(
new FileReader("F:nepalon"));
String s, s2 = new String();
while((s = Line()) != null)
s2 += s + "n";
e();
//1b. 接收鍵盤的輸入
BufferedReader stdin = new BufferedReader(new InputStreamReader());
tln("Enter a line:");
tln(Line());
//2. 從一個String物件中讀取資料
StringReader in2 = new StringReader(s2);
int c;
while((c = ()) != -1)
tln((char)c);
e();
//3. 從記憶體取出格式化輸入
try{
DataInputStream in3 =new DataInputStream(new ByteArrayInputStream(ytes()));
while(true)
tln((char)Byte());
}
catch(EOFException e){
tln("End of stream");
}
//4. 輸出到檔案
try{
BufferedReader in4 =new BufferedReader(new StringReader(s2));
PrintWriter out1 =new PrintWriter(new BufferedWriter(new FileWriter("F:nepalon")));
int lineCount = 1;
while((s = Line()) != null)
tln(lineCount++ + ":" + s);
e();
e();
}
catch(EOFException ex){
tln("End of stream");
}
//5. 資料的儲存和恢復
try{
DataOutputStream out2 =new DataOutputStream(new BufferedOutputStream(
new FileOutputStream("F:nepalon ")));
eDouble(3.1415926);
eChars("nThas was pi:writeCharsn");
eBytes("Thas was pi:writeByten");
e();
DataInputStream in5 =new DataInputStream(
new BufferedInputStream(new FileInputStream("F:nepalon ")));
BufferedReader in5br =new BufferedReader(new InputStreamReader(in5));
tln(Double());
tln(Line());
tln(Line());
}
catch(EOFException e){
tln("End of stream");
}
//6. 通過RandomAccessFile操作文件
RandomAccessFile rf = new RandomAccessFile("F:nepalon ",>
for(int i=0; i <10; i++)
eDouble(i*1.414);
e();
rf = new RandomAccessFile("F:nepalon ",>
for(int i=0; i <10; i++)
tln("Value " + i + ":" + Double());
e();
rf = new RandomAccessFile("F:nepalon ",>
(5*8);
eDouble(47.0001);
e();
rf = new RandomAccessFile("F:nepalon ",>
for(int i=0; i <10; i++)
tln("Value " + i + ":" + Double());
e();
}
}
關於程式碼的解釋(以區為單位):
1區中,當讀取檔案時,先把檔案內容讀到快取中,當呼叫Line()時,再從快取中以字元的方式讀取資料(以下簡稱“快取位元組讀取方式”)。
1b區中,由於想以快取位元組讀取方式從標準IO(鍵盤)中讀取資料,所以要先把標準IO()轉換成字元導向的stream,再進行BufferedReader封裝。
2區中,要以字元的形式從一個String物件中讀取資料,所以要產生一個StringReader型別的stream.