天天看點

Java中的I/O流

目錄

​​I/O流​​

​​輸入流(FileInputStream)​​

​​位元組流的讀取​​

​​字元流的讀取​​

​​輸出流(OutputStream)​​

​​帶緩沖區的輸入輸出流(BufferedInput/OutputStream)​​

I/O流

I/O流是資料傳送的通道,I/O流分為輸入流(Input)和輸出流(Ouput)。程式從外部讀取稱為輸入(Input),程式向外部寫成為輸出(Output)。

  • Java.io 包中幾乎包含了所有操作 輸入、輸出需要的類。所有這些類代表了輸入源和輸出目标。
  • Java.io 包中的流支援很多種格式,比如:基本類型、對象、本地化字元集等等。

一個流可以了解為一個資料的序列。輸入流表示從一個源讀取資料,輸出流表示向一個目标寫資料。

Java 為 I/O 提供了強大的而靈活的支援,使其更廣泛地應用到檔案傳輸和網絡程式設計中。

根據操作類型的不同可以分為:位元組流 和 字元流

輸入流(FileInputStream)

父類:InputStream

常用的位元組輸入流:FileInputStream ,繼承于InputStream

File file = new File("1.txt");       
FileInputStream ff = new FileInputStream(file);      

如果要讀取的檔案不存在,會報錯: java.io.FileNotFoundException 

位元組流的讀取

read()方法,讀取一個位元組,傳回該位元組的值,如果到達檔案末尾,則傳回-1。read()方法和疊代器一樣,會自動下移。

字元流的讀取

read(byte[])方法,從輸入流中讀取至多一個數組長度的内容,如果到達檔案末尾,則傳回-1。read()方法和疊代器一樣,會自動下移。

  • 數組稱為緩沖區數組,大小一般取值為1024的整數倍。
  • 轉換為字元時,使用String(byte [ ]   bytes,int offset,int length)
  • available()沒有讀取的剩餘位元組數,如果該檔案還從未被讀取,就傳回該檔案的長度。
  • close() 關閉流并釋放資源
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("1.txt");

        System.out.println("讀取位元組流的第一個字元:");
        FileInputStream ff = new FileInputStream(file);
        int a = ff.read();  //讀取的是1.txt檔案的第一個字元的ascii值
        System.out.println(a);
        ff.close();

        //位元組流的循環讀取
        System.out.println("讀取位元組流:");
        FileInputStream mm = new FileInputStream(file);
        int b;
        while( (b=mm.read())!=-1){
            System.out.print(b+"-");
        }
        System.out.println("");
        mm.close();

        //字元流的循環讀取
        System.out.println("讀取字元流:");
        FileInputStream nn = new FileInputStream(file);
        byte[] by = new byte[1024];
        int c;
        while((c= nn.read(by))!=-1){
            String str = new String(by,0,c);
            System.out.println(str);
        }
        nn.close();
    }
}      

輸出流(OutputStream)

父類:OutputStream

常用的位元組輸出流:FileOutputStream ,OutputStream

File file = new File("1.txt");
//覆寫
FileOutputStream ff = new FileOutputStream(file,false);
//追加
FileOutputStream ff = new FileOutputStream(file,true);      

如果父目錄不存在,會報FileNotFoundException異常,如果父目錄存在,會建立一個新的檔案,如果此時已經有檔案存在,會覆寫原檔案

常用方法:

//向檔案中寫入一個位元組的值
write(int x) 
//向檔案中寫入一個數組的資料
write(byte[]) 
//将偏移量為offset的索引位置長度為len的資料寫入檔案中
write(byte[],offset,len)


//關閉該輸出流
close()      

示例代碼

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("1.txt");   //如果本目錄下無1.txt檔案,則會建立一個1.txt檔案
        //覆寫
        FileOutputStream ff = new FileOutputStream(file,false);
        ff.write("hello,word!".getBytes());
        ff.close();
        //追加
        FileOutputStream fff = new FileOutputStream(file,true);
        fff.write("\n".getBytes());          //寫入換行符
        fff.write("加油,中國!".getBytes());
        fff.close();

        //檔案複制,将1.txt的内容複制到2.txt内容去
        FileInputStream aa = new FileInputStream(file);
        FileOutputStream bb = new FileOutputStream(new File("2.txt"));
        byte[] cc = new byte[1024];
        int count;
        while((count=aa.read(cc))!=-1){
            bb.write(cc,0,count);
        }
        aa.close();
        bb.close();
    }
}      

帶緩沖區的輸入輸出流(BufferedInput/OutputStream)

帶緩沖區的輸入輸出流讀寫檔案速度快于位元組流,java 上提供了專門帶緩沖區的位元組流,用以提高讀寫速度。

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        //讀取
        File f = new File("1.txt");

            //讀取位元組
        BufferedInputStream fff = new BufferedInputStream(new FileInputStream(f));
        int b;
        while( (b=fff.read())!=-1){
            System.out.print(b+"-");
        }
        System.out.println("");
            //讀取字元
        BufferedInputStream mmm = new BufferedInputStream(new FileInputStream(f));
        byte[] by = new byte[1024];
        int c;
        while((c= mmm.read(by))!=-1){
            String str = new String(by,0,c);
            System.out.println(str);
        }

        //寫入字元
        File f2 = new File("2.txt");

        BufferedOutputStream nnn = new BufferedOutputStream(new FileOutputStream(f2,false));
        nnn.write("hello,word".getBytes());
        nnn.close();
        BufferedOutputStream ppp = new BufferedOutputStream(new FileOutputStream(f2,true));
        ppp.write("\n".getBytes());          //寫入換行符
        ppp.write("加油,中國!".getBytes());
        ppp.close();

        //檔案複制
        BufferedInputStream  rrr = new BufferedInputStream(new FileInputStream(f2));
        BufferedOutputStream qqq = new BufferedOutputStream(new FileOutputStream(new File("3.txt")));
        byte[] cc = new byte[1024];
        int count;
        while((count=rrr.read(cc))!=-1){
            qqq.write(cc,0,count);
        }
        rrr.close();
        qqq.close();
    }
}