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


    
    
    
    
  • 相关阅读:
    深入浅出:了解前后端分离优势、前后端接口联调以及优化问题
    深入浅出:了解JavaScript中的call,apply,bind的差别
    Vue2.0 搭建Vue脚手架(vue-cli)
    深入浅出:promise的各种用法
    深入浅出:了解常见的设计模式(闭包、垃圾回收机制)
    sql server xml 功能
    sqlite 用法
    PowerDesigner使用
    asp.net 开发注意的几点
    vue template
  • 原文地址:https://www.cnblogs.com/marcotan/p/4256944.html
Copyright © 2011-2022 走看看