Java Stream
stream應該是水龍頭里的水資源,InputStream:是一個出水龍頭(把水封裝在里頭)的一個實物對象,該對象的read方法呢,就想成這
個"出水龍頭"這一機制對象的開關鈕,你read或openStream(其他對象包容InputStream對象的對象方法)
一下呢,就等于打開了出水龍頭的按鈕,水就出來了,里頭封裝的水是什么性質的呢,
你就用相應的容器來裝,如string或byte[].....
OutputStream:你就在InputStream基礎上反著想就ok了
=====================================================================================
當然,我們可以在Inputstream和OutputStream數據源的基礎上,從實際需要觸發,
來重新封裝出不同性能機制的輸入、輸出流了,java.io包中提供了很豐富的輸入、輸出流對象,如:
基于字節流的stream:
DataOutputStream----DataInputStream:
FileOutputStream-----FileInputStream:
File->inputstream->...
...->outputstream->File
.............等,可以用InputStream和OutputStream從JDK文檔查閱
基于字符流的stream(典型的以write 和reader來標識的):
FileWriter---FileReader:
StringWriter---StringReader:
.........等,你自己可以用Writer和Reader從JDK文檔里頭查看說明
======================================================================================
InputStreamReader r = new InputStreamReader(System. in ); // InputStream from console
BufferedReader in = new BufferedReader ( r );
String line ;
while (( line = in . readLine ()) != null) {
System. out . println ( "> "+line );
}
FileReader r = new FileReader ( args [0]); // InputStream from file
BufferedReader in = new BufferedReader ( r );
String line ;
while (( line = in . readLine ()) != null) {
System. out . println ( "> "+line );
}
FileWriter w = new FileWriter ( args [0]); //OutputStream to file
BufferedWriter bw = new BufferedWriter (w);
PrintWriter out = new PrintWriter (bw);
out . println ( "dies" );
out . println ( " ... ist ein Log!" );
out . println ( "Ciao!" );
out . close (); // schliessen nicht vergessen !
===================================================================
FileInputStream是InputStream的子類,由名稱上就可以知道, FileInputStream主要就是從指定的File中讀取資料至目的地。
FileOutputStream是OutputStream的子類,顧名思義,FileOutputStream主要就是從來源地寫入資料至指定的File中。
標準輸入輸出串流物件在程式一開始就會開啟,但只有當您建立一個FileInputStream或FileOutputStream的實例時,實際的串流才會開啟,而不使用串流時,也必須自行關閉串流,以釋放與串流相依的系統資源。
下面這個程式可以復制檔案,程式先從來源檔案讀取資料至一個位元緩沖區中,然后再將位元陣列的資料寫入目的檔案:
- FileStreamDemo.java
package onlyfun.caterpillar;
import java.io.*;
public class FileStreamDemo {
public static void main(String[] args) {
try {
byte[] buffer = new byte[1024];
FileInputStream fileInputStream =
new FileInputStream(new File(args[0]));
FileOutputStream fileOutputStream =
new FileOutputStream(new File(args[1]));
System.out.println("復制檔案:" +
fileInputStream.available() + "位元組");
while(true) { // 從來源檔案讀取資料至緩沖區
if(fileInputStream.available() < 1024) {
int remain;
while((remain = fileInputStream.read())
!= -1) {
fileOutputStream.write(remain);
}
break;
}
else {
fileInputStream.read(buffer);
// 將陣列資料寫入目的檔案
fileOutputStream.write(buffer);
}
}
// 關閉串流
fileInputStream.close();
fileOutputStream.close();
System.out.println("復制完成");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(
"using: java FileStreamDemo src des");
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
這個程式示范了兩個 read() 方法,一個可以讀入指定長度的資料至陣列,一個一次可以讀入一個位元組,每次讀取之后,讀取的指標都會往前進,您使用available()方法獲得還有多少位元組可以讀取;除了使用File來建立FileInputStream、FileOutputStream的實例之外,您也可以直接使用字串指定路徑來建立。
不使用串流時,記得使用close()方法自行關閉串流,以釋放與串流相依的系統資源。
posted on 2008-05-19 15:21 Xiaobo Sun 閱讀(957) 評論(1) 編輯 收藏 所屬分類: Java