java io 视频_IO流读写(复制)视频文件练习(java)

该博客展示了如何使用Java IO进行视频文件的复制,通过比较四种不同的复制方法(基础复制、带缓冲区的复制、使用BufferedInputStream和BufferedOutputStream、以及进一步优化的缓冲方法)来探讨效率提升。实验中,视频文件大小为1,443,621字节。最终发现,使用BufferedInputStream和BufferedOutputStream结合缓冲数组的方法在效率上最优。" 131843638,366432,实时数据流分析:Bytewax与ydata提升数据质量,"['数据挖掘', '流处理', 'Python', '数据质量', '实时分析']
摘要由CSDN通过智能技术生成

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值