文件的复制通过字节流和缓冲流(Buffered)

2018-05-23  本文已影响8人  Joy_yang17

public class CopyBufferedFileByStream {

    public static void main(String[] args) {
     try {
        FileInputStream fileInputStream = new FileInputStream("learn.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("learn222.txt");
        
        byte b [] = new byte[1024];
        BufferedInputStream bis = 
                new BufferedInputStream(fileInputStream);
        BufferedOutputStream bos = 
                new BufferedOutputStream(fileOutputStream);
        
        int count = 0;
        long before = System.currentTimeMillis();
        while (bis.read(b) != -1) {
            bos.write(b);
            count++;
        }
        
        System.out.println(count);
        System.out.println(System.currentTimeMillis()-before);
        
//      byte b [] = new byte[1024];
//      while(fileInputStream.read(b)!= -1){
//             fileOutputStream.write(b);
//      }
        
        bos.close();
        bis.close();
        
        fileInputStream.close();
        fileOutputStream.close();
     
     } catch (Exception e) {
        e.printStackTrace();
    }
  
    }
}
上一篇下一篇

猜你喜欢

热点阅读