文件拷贝过程中使用文件流、缓冲流、转换流以及速率比较

本文通过三个不同的方法展示了如何在Java中进行文件复制,并比较了仅使用文件流、使用文件流结合缓冲流以及使用文件流、缓冲流和转换流时的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.oracle.huanchongliu;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


import org.junit.Test;


public class Test_huanchong {


/**
* 实用了文件流和缓冲流
*/
@Test
public void test1(){
File file=new File("E:\\冰与火之歌\\[电影天堂www.dy2018.net]冰与火之歌:权力的游戏第一季05集中英双字[1024分辨率].RMVB");
File file1=new File("E:\\复制1.RMVB");

long t1=System.currentTimeMillis();
long t2=0;

FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
fos=new FileOutputStream(file1);
bos=new BufferedOutputStream(fos);

byte[] by=new byte[1024];
int len=1024;
while((len=bis.read(by))!=-1){
bos.write(by, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

t2=System.currentTimeMillis();
System.out.println(t2-t1);


}
/**
* 什么也不使用的,只用了文件流
*/
@Test
public void test2(){
File file=new File("E:\\冰与火之歌\\[电影天堂www.dy2018.net]冰与火之歌:权力的游戏第一季05集中英双字[1024分辨率].RMVB");
File file1=new File("E:\\复制2.RMVB");

long t1=System.currentTimeMillis();
long t2=0;

FileInputStream fis=null;
FileOutputStream fos=null;

try {
fis=new FileInputStream(file);

fos=new FileOutputStream(file1);


byte[] by=new byte[1024];
int len=1024;
while((len=fis.read(by))!=-1){
fos.write(by, 0, len);
fos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

t2=System.currentTimeMillis();
System.out.println(t2-t1);


}
/**
* 使用了文件流、缓冲流、转换流
*/
@Test
public void test3(){
File file=new File("E:\\冰与火之歌\\[电影天堂www.dy2018.net]冰与火之歌:权力的游戏第一季05集中英双字[1024分辨率].RMVB");
File file1=new File("E:\\复制3.RMVB");

long t1=System.currentTimeMillis();
long t2=0;

FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader bis=null;


FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bos=null;

try {
fis=new FileInputStream(file);
isr=new InputStreamReader(fis);
bis=new BufferedReader(isr);

fos=new FileOutputStream(file1);
osw=new OutputStreamWriter(fos);
bos=new BufferedWriter(osw);

char[] by=new char[1024];
int len=1024;
while((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

t2=System.currentTimeMillis();
System.out.println(t2-t1);


}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值