java 四種方式實現字符流文件的拷貝對比
阿新 • • 發佈:2018-06-03
put In exception bytes public 字節緩沖區 tput code cep
將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 四種方式實現字符流文件的拷貝對比