IO读写(复制)视频练习

package com.chen.io1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**

  • IO练习,一个复制视频的类
  • 练习文件为大小1,443,621 字节的wmv视频文件
  • @author Administrator
  • */
    public class CopyVideo {
    /**

    • 方法一
    • 基础复制方法
    • 使用InputStream,OutputStream
    • 测试结果:基础复制花费时间:10367毫秒;
    • @param fromFileName
    • @param toFileName
      */
      public static void baseCopyMethod(String fromFileName,String toFileName) {
      long startTime = System.currentTimeMillis();
      InputStream in = null;
      OutputStream out = null;
      try {
      in = new FileInputStream(fromFileName);
      out = new FileOutputStream(toFileName);
      int tempRead ;
      while((tempRead = in.read())>-1) {
      out.write(tempRead);
      out.flush();
      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }finally {
      try {
      if(in!=null)in.close();
      if(out!=null)out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      long endTime = System.currentTimeMillis();
      long spendTime = endTime - startTime;
      System.out.println("方法一,基础复制花费时间:"+ spendTime + "毫秒;");
      }

    /**

    • 方法二
    • 使用InputStream,OutputStream,并增加字节数组作为缓冲区,提升效率
    • 方法二,增加缓存后复制用时:121毫秒
    • @param fromFileName
    • @param toFileName
      */
      public static void baseCopyMethod2(String fromFileName,String toFileName) {
      long startTime = System.currentTimeMillis();
      InputStream in = null;
      OutputStream out = null;

      try {
      in = new FileInputStream(fromFileName);
      out = new FileOutputStream(toFileName);
      byte[] buf = new byte[102];//缓冲区数组,减少硬盘的读取操作
      int tempRead ;
      while((tempRead=in.read(buf))>-1) {
      out.write(buf);
      out.flush();
      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }finally {
      try {
      if(in!=null)in.close();
      if(out!=null)out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      long endTime = System.currentTimeMillis();
      long spendTime = endTime - startTime;
      System.out.println("方法二,增加缓存后复制用时:"+ spendTime + "毫秒");
      }

    /**

    • 方法三
    • 方法三,使用BufferedInputStream和BufferedOutputStream
    • 方法三,使用BufferedInputStream和BufferedOutputStream后复制用时:8268毫秒
    • @param fromFileName
    • @param toFileName
      */
      public static void bufferedCopyMethod(String fromFileName,String toFileName ) {
      long startTime = System.currentTimeMillis();
      BufferedInputStream in = null;
      BufferedOutputStream out = null;
      try {
      in = new BufferedInputStream(new FileInputStream(fromFileName));
      out = new BufferedOutputStream(new FileOutputStream(toFileName));
      int readContent;
      while((readContent = in.read())!=-1) {
      out.write(readContent);
      out.flush();
      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }finally {
      try {
      if(in!=null)in.close();
      if(out!=null)out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      long endTime = System.currentTimeMillis();
      long spendTime = endTime - startTime;
      System.out.println("方法三,使用BufferedInputStream和BufferedOutputStream后复制用时:"+ spendTime + "毫秒");
      }

    /**

    • 方法四
    • 方法四,在BufferedInputStream和BufferedOutputStream基础上再次添加缓存数组
    • 方法四,BufferedInputStream和BufferedOutputStream基础上添加缓存数组后复制用时:16毫秒
    • @param fromFileName
    • @param toFileName
      */
      public static void bufferedCopyMethod2(String fromFileName,String toFileName ) {
      long startTime = System.currentTimeMillis();
      BufferedInputStream in = null;
      BufferedOutputStream out = null;
      try {
      in = new BufferedInputStream(new FileInputStream(fromFileName));
      out = new BufferedOutputStream(new FileOutputStream(toFileName));
      int readContent ;
      byte[] buf = new byte[1024];
      while((readContent=in.read(buf))!=-1) {
      out.write(buf);
      out.flush();
      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }finally {
      try {
      if(in!=null)in.close();
      if(out!=null)out.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      long endTime = System.currentTimeMillis();
      long spendTime = endTime - startTime;
      System.out.println("方法四,BufferedInputStream和BufferedOutputStream基础上添加缓存数组后复制用时:"+ spendTime + "毫秒");
      }

    public static void main(String[] args) {
    String fromFileName = "C:\Users\Administrator\Desktop\test.wmv";
    String toFileName = "C:\Users\Administrator\Desktop\test1.wmv";
    //baseCopyMethod(fromFileName,toFileName);
    //baseCopyMethod2(fromFileName,toFileName);
    //bufferedCopyMethod(fromFileName,toFileName);
    bufferedCopyMethod(fromFileName,toFileName);
    }
    }

猜你喜欢

转载自blog.51cto.com/12694001/2125377