1. 程式人生 > >BufferedInputStream,FileInputStream,FileChannel實現檔案拷貝

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