Java輸出輸入流復制文件所用時光比較。本站提示廣大學習愛好者:(Java輸出輸入流復制文件所用時光比較)文章只能為提供參考,不一定能成為您想要的結果。以下是Java輸出輸入流復制文件所用時光比較正文
空話不多說,症結代碼以下所述:
package com.edu.xynu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOUnitCopy { //按字節 public static void copyByByte(File srcFile,File destFile) throws IOException{ FileInputStream fis=new FileInputStream(srcFile); FileOutputStream fos=new FileOutputStream(destFile); int i; while((i=fis.read())!=-1){ fos.write(i); } fis.close(); fos.close(); } //按字節數組 public static void copyByByteArray(File srcFile,File destFile) throws IOException{ FileInputStream fis=new FileInputStream(srcFile); FileOutputStream fos=new FileOutputStream(destFile); byte []buf=new byte[10*1024]; int i; while((i=fis.read(buf, 0, buf.length))!=-1){ fos.write(buf, 0, i); } fis.close(); fos.close(); } //字節緩沖流 public static void copyByBuff(File srcFile,File destFile) throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); int i; while((i=bis.read())!=-1){ bos.write(i); } bos.flush(); bis.close(); bos.close(); } //字節數組批量讀取 緩沖輸入流寫入 public static void copyByBuffArray(File srcFile,File destFile) throws IOException{ FileInputStream bis=new FileInputStream(srcFile); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); byte [] buf=new byte[10*1024]; int len; while((len=bis.read(buf,0,buf.length))!=-1){ bos.write(buf,0,len); } bos.flush(); bis.close(); bos.close(); } } package com.edu.xynu; import java.io.File; import java.io.IOException; public class IOUnitsCopyTest { public static void main(String[] args) { // TODO Auto-generated method stub try { long start=System.currentTimeMillis(); // IOUnitCopy.copyByByte(new File("c:\\1.mp3"), new File( // "c:\\2.mp3"));//90713ms // IOUnitCopy.copyByByteArray(new File("c:\\1.mp3"), new File( // "c:\\3.mp3"));//41ms // IOUnitCopy.copyByBuff(new File("c:\\1.mp3"), new File( // "c:\\4.mp3"));//556ms // IOUnitCopy.copyByByteArray(new File("c:\\1.mp3"), new File( // "c:\\5.mp3"));//30ms long end=System.currentTimeMillis(); System.out.println(end-start); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
測試文件是