1. 程式人生 > >流與檔案(java流概述)

流與檔案(java流概述)

【在忙中偷閒,總結些東西,既方便自己,也方便有需要的你們,利人利己何樂而不為!

  再編輯流之前,先來個前言,有需要看看,不需要的自動略過】

[前言:Socket套接字,它也是java中一個類,由它來實現兩臺主機的互動、由它搭建起的一個輸入輸出流的管道;例如:瀏覽器向伺服器傳送請求,需要Socket架起輸出流管道,而伺服器響應瀏覽器傳送的請求,則需要由Socket架起輸入流管道,由輸入流把【瀏覽器傳送的請求資訊===>伺服器進行的接收請求===>處理請求給予響應】應響應回瀏覽器的資訊傳送到瀏覽器,這樣瀏覽器就顯示了相應的內容咯]



1.Java流概述:

(1).InputStream(位元組輸入流)
InputStream是位元組輸入流,InputStream是一個抽象類,所有繼承了InputStream的類都是位元組輸入流,主要了解以下子類即可:


例:
public static void main(String[] args) {
try {
//定義一個輸入位元組流
InputStream in = new FileInputStream("c:/cc.txt");
//只讀取一個位元組
System.out.println(in.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


例:正確的讀取檔案內容
public static void main(String[] args) {
InputStream in = null;
try {
//定義一個輸入位元組流
in = new FileInputStream("c:/cc.txt");
//建立一個臨時變數來儲存讀取的位元組
int temp = 0;
//不能這樣寫
//while(in.read()!=-1){
//System.out.println(in.read());
//}
//這樣寫可以讀取所有的位元組
while((temp=in.read())>0){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//釋放資源
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}




(2).OutputStream(位元組輸出流)
所有繼承了OutputStream都是位元組輸出流


注意:當呼叫flush方法時,在close關閉流時就不自動呼叫flush方法了,否則會自動呼叫flush方法。




例:
public static void main(String[] args) {
OutputStream out = null;
try {
//建立檔案的輸出位元組流
out = new FileOutputStream("c:/dd.txt");
//寫入
out.write(123);
//強制清空輸出流
out.flush();
System.out.println("success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//釋放資源
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

(3).字元流:每次讀取2個位元組(16位),reader  writer


Reader(字元輸入流)
所有繼承了Reader都是字元輸如流.


例:
public static void main(String[] args) {
Reader reader = null;
try {
//建立字元輸入流
reader = new FileReader("c:/cc.txt");
//讀取檔案的內容,按字元讀取
System.out.println(reader.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//釋放資源
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


(4).Writer(字元輸出流)
所有繼承了Writer都是字元輸出流


例:
public static void main(String[] args) {
Writer writer = null;
try {
//建立字元輸出流
writer = new FileWriter("c:/ff.txt");
//新增內容
writer.write("java1");
//追加內容
writer.append('a');
//強制清空輸出流
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//釋放資源
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}




參考原始碼:字元輸出流可以調節boolean型別來決定是否可以把內容追加到原檔案中
public FileWriter(String fileName, boolean append) throws IOException {
        super(new FileOutputStream(fileName, append));
}


例:
public static void main(String[] args) throws IOException {
//true表示可以追加到原檔案中,false表示不能追加到原檔案中
Writer w = new FileWriter("c:/fff.txt",true);
//追加的內容
w.append("hello");
w.flush();
w.close();
}

(5).檔案流主要分為:檔案位元組輸入流、檔案位元組輸出流、檔案字元輸入流、檔案字元輸出流
FileInputStream(檔案位元組輸入流)


檔案的複製:
public static void main(String[] args) {
//建立檔案的輸入和輸出流
InputStream in = null;
OutputStream out = null;
try {
//建立檔案型別的實現類
in = new FileInputStream("c:/cc.txt");
out = new FileOutputStream("d:/xiu.txt");
//迴圈把檔案寫到輸出流中
int tmp = 0;
//複製檔案
while((tmp=in.read())>0){
out.write(tmp);
}
//輸出流要強制重新整理
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//資源釋放
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

(6).緩衝流:
緩衝流主要是為了提高效率而存在的,減少物理讀取次數,緩衝流主要有:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter,並且BufferedReader提供了實用方法readLine(),可以直接讀取一行,BufferWriter提供了newLine()可以寫換行符。

例:緩衝流會涉及【裝飾模式】
public static void main(String[] args) throws IOException {
//建立輸入輸出型別
InputStream in = null;
OutputStream out = null;
//建立輸入和輸出緩衝流物件
in = new BufferedInputStream(new FileInputStream("c:/a.txt"));
out = new BufferedOutputStream(new FileOutputStream("c:/b.txt"));
//複製檔案
int tmp=0;
while((tmp=in.read())!=-1){
out.write(tmp);
}
//釋放資源
out.flush();
in.close();
out.close();
}