天天看點

使用緩沖輸入流和緩沖輸出流複制檔案

使用緩沖輸入流和緩沖輸出流複制檔案

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public classBufferedOutputStreamDemo {

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

        // TODO Auto-generated method stub

        BufferedInputStreambis = newBufferedInputStream(new FileInputStream("fis.txt"));

        BufferedOutputStreambos = newBufferedOutputStream(new FileOutputStream("fos.txt"));

        //方式一

//      intby = 0;

//      while((by= bis.read()) != -1){

//          bos.write(by);

//      }

//      System.out.println("--------------");

        //方式二

        byte[] bys = new byte[1024];

        int len = 0;

        while((len = bis.read(bys))!= -1){

            bos.write(bys);

        }

        bos.close();

        bis.close();

    }

}