Java拷贝文件的4种方式
2014-06-06 15:36
507 查看
java.io.File class doesn’t have any shortcut method to copy file from source to destination. Here we will learn about four different ways we can copy file in java.
1. Using Stream: This is the conventional way of file copy in java, here we create two Files, source and destination. Then we create InputStream from source and write it to destination
file using OutputStream.
Here is the method that can be used to copy file using streams.
2. Using java.nio.channels.FileChannel: Java NIO classes were introduced in Java 1.4 and FileChannel can be used to copy file in java. According to transferFrom() method
javadoc, this way of copy file is supposed to be faster than using Streams to copy files.
Here is the method that can be used to copy file using FileChannel.
3. Using Apache Commons IO: Apache Commons IO FileUtils.copyFile(File srcFile, File destFile) can be used to copy file in java. If you are already using Apache Commons IO in your
project, it makes sense to use this for code simplicity. Internally it uses Java NIO FileChannel, so you can avoid this wrapper method if you are not already using it for other functions.
Here is the method for using apache commons io for file copy in java.
4. Java 7 Files class: If you are working on Java 7, you can use Files class copy() method to copy file in java. It uses File System providers to copy the files.
Now to find out which is the fastest method, I wrote a test class and executed above methods one-by-one for copy file of 1 GB. In each call, I used different files to avoid any benefit to later methods because of caching.
Here is the output of above program, note that I commented above code to make sure every time only one method is used for file copy.
Time taken by Stream Copy 1x
Time taken by Channel Copy 2.34x
Time taken by Apache Commons IO Copy 2.43x
Time taken by Java7 Files Copy 2.00x
From the output it’s clear that Stream Copy is the best way to copy File in Java.
1. Using Stream: This is the conventional way of file copy in java, here we create two Files, source and destination. Then we create InputStream from source and write it to destination
file using OutputStream.
Here is the method that can be used to copy file using streams.
private static void copyFileUsingStream(File source, File dest) throws IOException { InputStream is = null; OutputStream os = null; try { is = new FileInputStream(source); os = new FileOutputStream(dest); byte[] buffer =new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer,0, length); } } finally{ is.close(); os.close(); } }
2. Using java.nio.channels.FileChannel: Java NIO classes were introduced in Java 1.4 and FileChannel can be used to copy file in java. According to transferFrom() method
javadoc, this way of copy file is supposed to be faster than using Streams to copy files.
Here is the method that can be used to copy file using FileChannel.
private static void copyFileUsingChannel(File source, File dest) throws IOException { FileChannel sourceChannel =null; FileChannel destChannel =null; try { sourceChannel =new FileInputStream(source).getChannel(); destChannel = new FileOutputStream(dest).getChannel(); destChannel.transferFrom(sourceChannel,0, sourceChannel.size()); }finally{ sourceChannel.close(); destChannel.close(); } }
3. Using Apache Commons IO: Apache Commons IO FileUtils.copyFile(File srcFile, File destFile) can be used to copy file in java. If you are already using Apache Commons IO in your
project, it makes sense to use this for code simplicity. Internally it uses Java NIO FileChannel, so you can avoid this wrapper method if you are not already using it for other functions.
Here is the method for using apache commons io for file copy in java.
private static void copyFileUsingApacheCommonsIO(File source, File dest)throws IOException { FileUtils.copyFile(source, dest); }
4. Java 7 Files class: If you are working on Java 7, you can use Files class copy() method to copy file in java. It uses File System providers to copy the files.
private static void copyFileUsingJava7Files(File source, File dest)throws IOException { Files.copy(source.toPath(), dest.toPath()); }
Now to find out which is the fastest method, I wrote a test class and executed above methods one-by-one for copy file of 1 GB. In each call, I used different files to avoid any benefit to later methods because of caching.
Here is the output of above program, note that I commented above code to make sure every time only one method is used for file copy.
Time taken by Stream Copy 1x
Time taken by Channel Copy 2.34x
Time taken by Apache Commons IO Copy 2.43x
Time taken by Java7 Files Copy 2.00x
From the output it’s clear that Stream Copy is the best way to copy File in Java.
相关文章推荐
- java复制文件的4种方式及拷贝文件到另一个目录下的实例代码
- Java实现文件拷贝的4种方法(转)
- Java学习笔记之swing文件选取器 例子 (4种读写文件方式-和图片的读写方法)
- java复制文件的4种方式
- java复制文件的4种方式
- java复制文件的4种方式
- JAVA读取PROPERTIY文件/JAVA拷贝一个对象的方式
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- java 4种方式读取配置文件 + 修改配置文件
- Java实现文件拷贝的4种方法
- java 4种方式读取配置文件 + 修改配置文件
- Java实现文件拷贝的4种方法
- Java实现文件拷贝的4种方法
- Java实现文件拷贝的4种方法
- Java实现文件拷贝的4种方法.
- Java实现文件拷贝的4种方法.
- Java实现文件拷贝的4种方法.
- java数组复制的4种方式,java数组拷贝问题
- 4种java复制文件的方式
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)