標籤:軟體 row output 位元組流 rgs long leo 方式 TE
將D:\\應用軟體\\vm.exe 拷貝到C:\\vm.exe 四種方法耗費時間對比 4>2>3>1
package Copy;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* * 檔案複製方式,位元組流,一個4個方式 * 1.位元組流讀寫單個位元組 * 2.位元組流讀寫位元組數組 * 3.位元組緩衝區讀寫單個位元組 * 4.位元組緩衝區讀寫位元組數組 */public class Copy { public static void main(String[] args) throws IOException { long s=System.currentTimeMillis(); copy_4(new File("E:\\應用軟體\\vm.exe"),new File("c:\\vm.exe")); long e=System.currentTimeMillis(); System.out.println(e-s); } /* * 方法:實現檔案複製 * 4.位元組流緩衝區流讀寫位元組數組 */ public static void copy_4(File src,File desc)throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc)); int len=0; byte[] bytes=new byte[1024]; while((len=bis.read(bytes))!=-1){ bos.write(bytes,0,len); } bos.close(); bis.close(); } /* * 方法:實現檔案複製 * 3,位元組流緩衝區流讀寫單個位元組 */ public static void copy_3(File src,File desc)throws IOException{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc)); int len=0; while((len=bis.read())!=-1){ bos.write(len); } bos.close(); bis.close(); } /* * 方法:實現複製 * 2.位元組流讀寫位元組數組 */ public static void copy_2(File src,File desc)throws IOException{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(desc); int len=0; byte[] bytes=new byte[1024]; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } fos.close(); fis.close(); } /* * 方法:實現檔案複製 * 1.位元組流讀寫單個位元組 */ public static void copy_1(File src,File desc) throws IOException{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(desc); int len=0; while((len=fis.read())!=-1){ fos.write( len); } fos.close(); fis.close(); }}
java 四種方式實現字元流檔案的拷貝對比