当前位置:网站首页 > Java教程 > 正文

java复制视频教程



packagecom.chen.io1;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;/* IO练习,一个复制视频的类

* 练习文件为大小1,443,621 字节的wmv视频文件

*@authorAdministrator

/

public classCopyVideo {/* 方法一

* 基础复制方法

* 使用InputStream,OutputStream

* 测试结果:基础复制花费时间:10367毫秒;

*@paramfromFileName

*@paramtoFileName*/

public static voidbaseCopyMethod(String fromFileName,String toFileName) {long startTime =System.currentTimeMillis();

InputStream in= null;

OutputStream out= null;try{

in= newFileInputStream(fromFileName);

out= newFileOutputStream(toFileName);inttempRead ;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毫秒

*@paramfromFileName

*@paramtoFileName*/

public static voidbaseCopyMethod2(String fromFileName,String toFileName) {long startTime =System.currentTimeMillis();

InputStream in= null;

OutputStream out= null;try{

in= newFileInputStream(fromFileName);

out= newFileOutputStream(toFileName);byte[] buf = new byte[102];//缓冲区数组,减少硬盘的读取操作

inttempRead ;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毫秒

*@paramfromFileName

*@paramtoFileName*/

public static voidbufferedCopyMethod(String fromFileName,String toFileName ) {long startTime =System.currentTimeMillis();

BufferedInputStream in= null;

BufferedOutputStream out= null;try{

in= new BufferedInputStream(newFileInputStream(fromFileName));

out= new BufferedOutputStream(newFileOutputStream(toFileName));intreadContent;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毫秒

*@paramfromFileName

*@paramtoFileName*/

public static voidbufferedCopyMethod2(String fromFileName,String toFileName ) {long startTime =System.currentTimeMillis();

BufferedInputStream in= null;

BufferedOutputStream out= null;try{

in= new BufferedInputStream(newFileInputStream(fromFileName));

out= new BufferedOutputStream(newFileOutputStream(toFileName));intreadContent ;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 voidmain(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);

}

}

版权声明


相关文章:

  • java安装打开教程2024-12-18 12:58:06
  • 刷石器教程java2024-12-18 12:58:06
  • java入门教程442024-12-18 12:58:06
  • java多级联动教程2024-12-18 12:58:06
  • java编码环境教程2024-12-18 12:58:06
  • 2017淘宝JAVA视频教程2024-12-18 12:58:06
  • 计算机java教程 pdf2024-12-18 12:58:06
  • java教程419集2024-12-18 12:58:06
  • java实验教程题库2024-12-18 12:58:06
  • java 视屏教程2024-12-18 12:58:06