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

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

作者: Joy_yang17 | 来源:发表于2018-05-23 19:16 被阅读8次

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();
    }
  
    }
}

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/qaldjftx.html