Java: 复制文件最快方法
2014-04-15 16:00
246 查看
原文地址:
http://webcenterol.iteye.com/blog/1231259
不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):
如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):
常用的方法1是:
常用的方法2是:
http://webcenterol.iteye.com/blog/1231259
不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):
private static void nioTransferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }
private static void nioTransferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }
如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):
private static void nioBufferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }
private static void nioBufferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }
常用的方法1是:
private static void customBufferBufferedStreamCopy(File source, File target) { InputStream fis = null; OutputStream fos = null; try { fis = new BufferedInputStream(new FileInputStream(source)); fos = new BufferedOutputStream(new FileOutputStream(target)); byte[] buf = new byte[4096]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { close(fis); close(fos); } }
private static void customBufferBufferedStreamCopy(File source, File target) { InputStream fis = null; OutputStream fos = null; try { fis = new BufferedInputStream(new FileInputStream(source)); fos = new BufferedOutputStream(new FileOutputStream(target)); byte[] buf = new byte[4096]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { close(fis); close(fos); } }
常用的方法2是:
private static void customBufferStreamCopy(File source, File target) { InputStream fis = null; OutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); byte[] buf = new byte[4096]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { close(fis); close(fos); } }
相关文章推荐
- 【Java学习笔记】基础知识学习19【一个大文件的复制方法】
- Java 高效复制文件方法
- java大文件复制最高效方法:多线程FileChannel
- Java: 复制文件最快方法
- java高效的文件复制方法
- Java IO学习【13】利用字节流复制文件的4种方法详解
- java文件复制最快的方法!
- Java: 复制文件最快方法
- Java: 复制文件最快、高效率的方法
- Java中的4种文件复制方法
- Java: 复制文件最快方法
- 将某盘的java文件复制到目标文件的方法。
- Java 复制文件的高效方法
- 黑马程序员——java-使用IO流复制文件主要的五种方法
- java写的一个用字节流复制文件的方法
- java: 复制文件最快方法
- JAVA NIO(三) 三种文件的复制方法与效率对比
- java中复制文件的新方法
- 【Java】利用文件通道的方式改进Java复制单个文件的方法
- Java-->用递归方法复制目录、子目录以及文件(文件用到IO流)