zoukankan      html  css  js  c++  java
  • BufferedInputStream,FileInputStream,FileChannel实现文件拷贝

    从上篇文章中知道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();
    	}
    }


    
    
    
    
  • 相关阅读:
    常用的几个vagrant命令
    window下sh文件在linux转码
    centos 7 免密登录
    centos7系列-给普通用户sudo权限
    Hadoop hdfs完全分布式搭建教程
    ribbon的注解使用报错--No instances available for [IP]
    修改VirtualBox虚拟机默认存储路径及虚拟机迁移方法
    【SpringMVC】文件上传Expected MultipartHttpServletRequest: is a MultipartResolver错误解决
    发现不错待研究的技术
    android的开发 华为手机上不显示menu键
  • 原文地址:https://www.cnblogs.com/marcotan/p/4256944.html
Copyright © 2011-2022 走看看