您的位置:首页 > Web前端

BufferedInputStream,FileInputStream,FileChannel实现文件拷贝

2014-08-27 10:04 429 查看
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率。下面的例子中实现了用BufferedInputStream与FileInputStream实现20M文件的差异
<pre name="code" class="java">public class BufferedOutputStreamDemo {

/**
* 用BufferedInputStream, BufferedOutputStream实现文件拷贝
* @throws IOException
*/
@Test
public void test1() throws IOException{
File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt");
File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt");
targetFile.deleteOnExit();
targetFile.createNewFile();
InputStream inputStream = new FileInputStream(originFile);
OutputStream outputStream = new FileOutputStream(targetFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
long length = originFile.length();
double size = length/1024/1024;
int temp = 0;
byte b[] = new byte[(int)originFile.length()] ;
long startTime = System.nanoTime();
//此种方法拷贝20M文件耗时约1451ms
/*while((temp =bufferedInputStream.read()) != -1){
bufferedOutputStream.write(temp);
}*/
//此种方法拷贝20M文件耗时约146ms
while(bufferedInputStream.read(b, 0, b.length) != -1){
bufferedOutputStream.write(b, 0, b.length);
}
long endTime = System.nanoTime();
System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms");
bufferedInputStream.close();
//bufferedOutputStream.close();
}

/**
* 用FileInputStream和FileOutputStream实现文件拷贝
* @throws IOException
*/
@Test
public void test2() throws IOException{
File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt");
File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt");
targetFile.deleteOnExit();
targetFile.createNewFile();
FileInputStream inputStream = new FileInputStream(originFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
int temp = 0;
long length = originFile.length();
byte[] byteBuffer = new byte[(int) length];
double size = length/1024/1024;
long startTime = System.nanoTime();
//此种方法拷贝20M文件几分钟
/*while((temp =inputStream.read()) != -1){
outputStream.write(temp);
}*/
while(inputStream.read(byteBuffer, 0, (int)length) != -1){
outputStream.write(byteBuffer, 0, (int)length);
}
long endTime = System.nanoTime();
System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms");
inputStream.close();
outputStream.close();
}

/**
* 用FileChannel实现文件拷贝
* @throws IOException
*/
@Test
public void test3() throws IOException{
File originFile = new File("D:"+File.separator+"test"+File.separator+"bufferedStream_copy.txt");
File targetFile = new File("D:"+File.separator+"test"+File.separator+"copy"+File.separator+"bufferedStream_copy.txt");
targetFile.deleteOnExit();
targetFile.createNewFile();
FileInputStream inputStream = new FileInputStream(originFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
FileChannel inputChannel = inputStream.getChannel();
FileChannel outputChannel = outputStream.getChannel();
long length = originFile.length();
double size = length/1024/1024;
long startTime = System.nanoTime();
inputChannel.transferTo(0, length, outputChannel);
long endTime = System.nanoTime();
System.out.println("copy大小为"+size+"M文件耗费时间:"+(endTime-startTime)/1000000+"ms");
inputStream.close();
outputStream.close();
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: