當前位置:
首頁 >
Java IO系列之字节流拷贝文件性能比较
發布時間:2023/11/27
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Java IO系列之字节流拷贝文件性能比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?Java IO 字節流基類
?InputStream--輸入流,
?OutPutStream--輸出流,
?輸入流用于讀,輸出流用于寫.
?字節流默認一次只讀取或輸出一個字節。?
?
package jonavin.io;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 IOUtil {/*** 文件拷貝-- 一個字節一個字節拷貝* @param srcFile* @param destFile*/public static void copyFileByByte(File srcFile,File destFile) throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目標文件int b;while ((b = in.read()) != -1) {out.write(b);//寫入一個字節的低八位 }in.close();out.close();}/*** 拷貝文件一次讀取多個字節*/public static void copyFileByByteBuf(File srcFile,File destFile) throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目標文件int b;byte[] buf = new byte[8*1024];//開辟8K的緩存while ((b = in.read(buf,0,buf.length))!=-1) {out.write(buf, 0, b);}in.close();out.close();}/*** 通過緩沖流copy文件* @throws IOException*/public static void copyFileByBuffed(File srcFile,File destFile)throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目標文件BufferedInputStream inbuf = new BufferedInputStream(in);BufferedOutputStream outbuf = new BufferedOutputStream(out);int b;while ((b = inbuf.read())!=-1) {outbuf.write(b);}in.close();out.close();inbuf.close();outbuf.close();}/*** 通過緩沖流copy文件 緩沖流一次讀取多個字節* @throws IOException*/public static void copyFileByBuffedBuf(File srcFile,File destFile)throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目標文件BufferedInputStream inbuf = new BufferedInputStream(in);BufferedOutputStream outbuf = new BufferedOutputStream(out);int b;byte[] buf = new byte[8*1024];while ((b = inbuf.read(buf,0,buf.length))!=-1) {outbuf.write(buf,0,b);}in.close();out.close();inbuf.close();outbuf.close();}/*** 文件拷貝* @param srcpath* @param destpath* @throws IOException*/public static void copyFile(String srcpath,String destpath)throws IOException{copyFileByBuffedBuf(new File(srcpath), new File(destpath));} }
調用 : IOUtil.copyFile("C:\\apache-tomcat-7.0.57.zip", "c:\\ret.dat");//源文件大小9M
copyFileByByte? ----》 62229 毫秒
copyFileByByteBuf -----》 97 毫秒
copyFileByBuffed -----》 457 毫秒
copyFileByBuffedBuf ----》 28 毫秒? 效率最好
轉載于:https://www.cnblogs.com/jonavin/p/4165641.html
總結
以上是生活随笔為你收集整理的Java IO系列之字节流拷贝文件性能比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原神鸣神寻踪怎么触发?
- 下一篇: eclipse如何卸载adt插件