How to copy files in Java
copy
file
filechannel
files
There are several ways you can perform a file copying operation in Java and we will discuss three of the most popular in this example. The example excludes any third party library like Apache Commons IO.
Copy using Streams
This is the classic way to copy the content of a file to another. You simply read a number of bytes from File A using FileInputStream and write them to File B using FileOutputStream.
private static void copyStream(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);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
input.close();
if (output != null)
output.close();
}
}
Copy using FileChannels
Class java.nio.channels.FileChannel
contains a method to copy files. The class was introduced since Java 1.4.
private static void copyFileChannels(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());
} catch (Exception e) {
e.printStackTrace();
} finally {
inputChannel.close();
outputChannel.close();
}
}
Copy using Files
Since Java 1.7 was introduces java.nio.file.Files
class that consists exclusively of static methods that operate on files, directories, or other types of files.
private static void copyFiles(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
Full Example
package com.admfactory.io;
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;
public class CopyFile {
private static void copyStream(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);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
input.close();
if (output != null)
output.close();
}
}
private static void copyFileChannels(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());
} catch (Exception e) {
e.printStackTrace();
} finally {
inputChannel.close();
outputChannel.close();
}
}
private static void copyFiles(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
public static void main(String[] args) throws Exception {
System.out.println("Copy Files Example");
System.out.println();
File sF1 = new File("D:\\admfactory.com\\dir1\\f1.txt");
File sF2 = new File("D:\\admfactory.com\\dir1\\f2.txt");
File sF3 = new File("D:\\admfactory.com\\dir1\\f3.txt");
File dF1 = new File("D:\\admfactory.com\\dir2\\f1.txt");
File dF2 = new File("D:\\admfactory.com\\dir2\\f2.txt");
File dF3 = new File("D:\\admfactory.com\\dir2\\f3.txt");
System.out.println("Copy file using standard stream");
System.out.println("Source: " + sF1.getAbsolutePath());
System.out.println("Destination: " + dF1.getAbsolutePath());
System.out.println();
copyStream(sF1, dF1);
System.out.println("Copy file using FileChannels");
System.out.println("Source: " + sF2.getAbsolutePath());
System.out.println("Destination: " + dF2.getAbsolutePath());
System.out.println();
copyFileChannels(sF2, dF2);
System.out.println("Copy file using Files");
System.out.println("Source: " + sF3.getAbsolutePath());
System.out.println("Destination: " + dF3.getAbsolutePath());
System.out.println();
copyFiles(sF3, dF3);
}
}
Output
Copy Files Example
Copy file using standard stream
Source: D:\admfactory.com\dir1\f1.txt
Destination: D:\admfactory.com\dir2\f1.txt
Copy file using FileChannels
Source: D:\admfactory.com\dir1\f2.txt
Destination: D:\admfactory.com\dir2\f2.txt
Copy file using Files
Source: D:\admfactory.com\dir1\f3.txt
Destination: D:\admfactory.com\dir2\f3.txt