Java-IO流-文件复制2
package cn.bruce.IO; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOCopyDemo1 { // 文件复制方式的4种 比较 // 1、单字节流复制 // 2、字节数组复制 // 3、字节流缓冲流单字节复制 // 4、字节流缓冲区数组复制 public static void main(String[] args) throws IOException { FileInputStream Zfin = new FileInputStream("E:\\a.avi"); FileInputStream Zfin1 = new FileInputStream("E:\\a.avi"); FileInputStream Zfin2 = new FileInputStream("E:\\a.avi"); FileInputStream Zfin3 = new FileInputStream("E:\\a.avi"); FileOutputStream Zfou = new FileOutputStream("E:\\dzjl.avi");// 单字节 FileOutputStream Zfou1 = new FileOutputStream("E:\\ZJSZ.avi");// 字节数组 FileOutputStream Zfou2 = new FileOutputStream("E:\\dzjlHC.avi");// 单字节缓冲 FileOutputStream Zfou3 = new FileOutputStream("E:\\ZJSZHC.avi");// 字节数组缓冲 long s = System.currentTimeMillis(); fun1(Zfin, Zfou);// 一个一个字节的复制,太慢了 long e = System.currentTimeMillis(); System.out.println(e - s); long s1 = System.currentTimeMillis(); fun2(Zfin1, Zfou1);// 字节数组形式的复制 long e1 = System.currentTimeMillis(); System.out.println(e1 - s1); long s2 = System.currentTimeMillis(); fun2(Zfin2, Zfou2);// 单字节缓冲形式的复制 long e2 = System.currentTimeMillis(); System.out.println(e2 - s2); long s3 = System.currentTimeMillis(); fun2(Zfin3, Zfou3);// 字节数组缓冲形式的复制 long e3 = System.currentTimeMillis(); System.out.println(e3 - s3); } // 1、单字节流复制 public static void fun1(FileInputStream fin, FileOutputStream fou) { try { int len = 0; while ((len = fin.read()) != -1) { fou.write(len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fou != null) { fou.close();// 关输出流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输出流关闭失败"); } finally { try { if (fin != null) { fin.close();// 关输入流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输入流关闭失败"); } } } } // 2、字节数组复制 public static void fun2(FileInputStream fin, FileOutputStream fou) { try { int len = 0; byte[] bs = new byte[1024]; while ((len = fin.read(bs)) != -1) { fou.write(bs, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fou != null) { fou.close();// 关输出流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输出流关闭失败"); } finally { try { if (fin != null) { fin.close();// 关输入流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输入流关闭失败"); } } } } // 3、字节流缓冲流单字节复制 public static void fun3(FileInputStream fin, FileOutputStream fou) throws IOException { BufferedInputStream bin = new BufferedInputStream(fin); BufferedOutputStream bou = new BufferedOutputStream(fou); try { int len = 0; while ((len = bin.read()) != -1) { bou.write(len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fou != null) { fou.close();// 关输出流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输出流关闭失败"); } finally { try { if (fin != null) { fin.close();// 关输入流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输入流关闭失败"); } } } } // 4、字节流缓冲区数组复制 public static void fun4(FileInputStream fin, FileOutputStream fou) throws IOException { BufferedInputStream bin = new BufferedInputStream(fin); BufferedOutputStream bou = new BufferedOutputStream(fou); try { int len = 0; byte[] bs = new byte[1024]; while ((len = bin.read(bs)) != -1) { bou.write(bs, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fou != null) { fou.close();// 关输出流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输出流关闭失败"); } finally { try { if (fin != null) { fin.close();// 关输入流 } } catch (IOException e2) { e2.printStackTrace(); throw new RuntimeException("输入流关闭失败"); } } } } }