天天看點

[Java] FileInputStream/FileOutputStream 位元組流

import java.io.*;

public class CopyFileTest{

    public static void main(String[] args) throws IOException{

        //源檔案定義為位元組輸入流

        FileInputStream fis = new FileInputStream("1.jpg");

        //把目标檔案定義為位元組輸出流

        FileOutputStream fos = new FileOutputStream("temp.jpg");

        //從位元組輸入流中讀取位元組

        int read = fis.read();

        //将位元組輸入到輸出流中,空位元組為 -1

        while(read!=-1){

            //寫到檔案中

            fos.write(read);

            //讀取下一個位元組

            read = fis.read();

        }

        //清空輸出流

        fos.flush();

        //關閉輸出流

        fos.close();

    }

}