What are the ways to copy files in java
This article mainly introduces the way of copying files in java, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
Four ways of File replication in java
1. Copy using FileStreams
A more classic code that uses FileInputStream to read the bytes of file An and FileOutputStream to write to file B.
Public static void copy (String source, String dest, int bufferSize) {InputStream in = null; OutputStream out = null; try {in = new FileInputStream (new File (source)); out = new FileOutputStream (new File (dest)); byte [] buffer = new byte [bufferSize]; int len; while ((len = in.read (buffer)) > 0) {out.write (buffer, 0, len) } catch (Exception e) {Log.w (TAG + ": copy", "error occur while copy", e);} finally {safelyClose (TAG + ": copy", in); safelyClose (TAG + ": copy", out);} 2.2, use FileChannel
Java NIO includes transferFrom methods, which should be copied faster according to the document than the file stream.
Public static void copyNio (String source, String dest) {FileChannel input = null; FileChannel output = null; try {input = new FileInputStream (new File (from)). GetChannel (); output = new FileOutputStream (new File (to)). GetChannel (); output.transferFrom (input, 0, input.size ());} catch (Exception e) {Log.w (TAG + "copyNio", "error occur while copy", e) } finally {safelyClose (TAG + "copyNio", input); safelyClose (TAG + "copyNio", output);}} 3. Copying Appache Commons IO using Apache Commons IO provides a FileUtils.copyFile (File from, File to) method for file replication. If this class library is used in the project, this method is a good choice. Its interior is also implemented using Java NIO's FileChannel. The path to commons-io: http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html. There are also many practical methods, such as copying directories, copying files in specified formats, and so on. Private static void copyFileByApacheCommonsIO (File source, File dest) throws IOException {FileUtils.copyFile (source, dest);}
4. Copy the Files class using Java7
Private static void copyFileUsingJava7Files (File source, File dest) throws IOException {Files.copy (source.toPath (), dest.toPath ());}
I didn't test it myself. I looked for the netizens' test program and output, and the performance data are for your reference.
Import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.channels.FileChannel;import java.nio.file.Files;import org.apache.commons.io.FileUtils;public class CopyFilesExample {public static void main (String [] args) throws InterruptedException, IOException {File source = new File ("C:\\ Users\\ nikos7\ Desktop\\ files\\ sourcefile1.txt") File dest = new File ("C:\ Users\\ nikos7\ Desktop\\ files\\ destfile1.txt"); / / copy file using FileStreams long start = System.nanoTime (); long end; copyFileUsingFileStreams (source, dest); System.out.println ("Time taken by FileStreams Copy =" + (System.nanoTime ()-start)) / / copy files using java.nio.FileChannel source = new File ("C:\\ Users\ nikos7\\ Desktop\\ files\\ sourcefile2.txt"); dest = new File ("C:\\ Users\\ nikos7\\ Desktop\\ files\ destfile2.txt"); start = System.nanoTime (); copyFileUsingFileChannels (source, dest); end = System.nanoTime (); System.out.println ("Time taken by FileChannels Copy =" + (end-start)) / / copy file using Java7Files class source = new File ("C:\\ Users\ nikos7\\ Desktop\\ files\\ sourcefile3.txt"); dest = new File ("C:\\ Users\\ nikos7\\ Desktop\\ files\ destfile3.txt"); start = System.nanoTime (); copyFileUsingJava7Files (source, dest); end = System.nanoTime (); System.out.println ("Time taken by Java7Files Copy =" + (end-start)) / / copy files using apache commons io source = new File ("C:\\ Users\ nikos7\\ Desktop\\ files\\ sourcefile4.txt"); dest = new File ("C:\\ Users\\ nikos7\\ Desktop\\ files\ destfile4.txt"); start = System.nanoTime (); copyFileUsingApacheCommonsIO (source, dest); end = System.nanoTime (); System.out.println ("Time taken by ApacheCommonsIO Copy =" + (end-start)) } private static void copyFileUsingFileStreams (File source, File dest) throws IOException {InputStream input = null; OutputStream output = null; try {input = new FileInputStream (source); output = new FileOutputStream (dest); byte [] buf = new byte [1024]; int bytesRead; while ((bytesRead = input.read (buf)) > 0) {output.write (buf, 0, bytesRead) }} finally {input.close (); output.close ();}} private static void copyFileUsingFileChannels (File source, File dest) throws IOException {FileChannel inputChannel = null; FileChannel outputChannel = null; try {inputChannel = new FileInputStream (source). GetChannel (); outputChannel = new FileOutputStream (dest). GetChannel (); outputChannel.transferFrom (inputChannel, 0, inputChannel.size ());} finally {inputChannel.close () OutputChannel.close ();}} private static void copyFileUsingJava7Files (File source, File dest) throws IOException {Files.copy (source.toPath (), dest.toPath ());} private static void copyFileUsingApacheCommonsIO (File source, File dest) throws IOException {FileUtils.copyFile (source, dest);}}
Output:
Time taken by FileStreams Copy = 127572360
Time taken by FileChannels Copy = 10449963
Time taken by Java7 Files Copy = 10808333
Time taken by Apache Commons IO Copy = 17971677
Thank you for reading this article carefully. I hope the article "what are the ways to copy files in java" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!