• 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();
    	}
    }


    
    
    
    
  • 相关阅读:
    vs2017发布成功但是发布目录没有文件
    解决 CS0006 未能找到元数据文件
    EFPowertools 参数错误
    给WebAPI项目加上一个说明文档以及一个测试按钮
    Visual Studio 不显示SVN 状态图标解决方法
    JQuery PowerTimer 插件详解
    UML的各种关系理解
    C语言之如何上机运行第一个Hello World小程序
    打破 Serverless 落地边界,阿里云 SAE 发布 5 大新特性
    TCP/IP协议栈在Linux中内核中的运行时序分析
  • 原文地址:https://www.cnblogs.com/marcotan/p/4256944.html
Copyright © 2020-2023  润新知