Java Copy File Examples
In this tutorial, we will see how to copy the file to another location in Java.
In java development we have to deal with files, we need operation like file copy delete, etc.
This can be done in many ways as given below.
1). Using Input/Output Stream
2). Using java.nio.channels.FileChannel
3). Using Apache Commons IO
4). Using java7 Files.copy
5). Google's Guava library
We will see all of them.
1). Using Input/Output Stream
This is a simple core level way. We create 2 file objects source and destination. Then we create InputStream from source and write it to the destination file using OutputStream for java copy file operation.
Methods used in the program
int read();
Reads a byte of data. Present in FileInputStream.
Other versions of this method :
int read(byte[] bytearray) and
int read(byte[] bytearray, int offset, int length)
void write(int b)
Writes a byte of data. Present in FileOutputStream.
Other versions of this method :
void write(byte[] bytearray)
and
void write(byte[] bytearray, int offset, int length);
private static void copyFile(File source, File destination) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(destination);
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
This is a comparatively faster way to copy files.
private static void copyFile(File source, File destination) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(destination).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{
sourceChannel.close();
destChannel.close();
}
}
3). Using Apache Commons IO
Apache common Io library provides classes and methods to copy dir. It's pretty easy to use.
We need to include maven dependencies as given below.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
private static void copyFileUsingApacheCommonsIO(File source, File destination) throws IOException {
FileUtils.copyFile(source, destination);
}
4). Using java7 Files.copy
If you are using java7 or higher its simple to use.
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
5). Google's Guava library
Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more.
Maven Dependency
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
import com.google.common.io.Files;
public class FilecopyUsingGauva {
public static void main(String[] args) throws IOException {
File source = new File("hello.txt");
File destination = new File("hello2.txt");
Files.copy(source, destination);
}
}
How to get a folder name from the file path in java?
private void getFolderName()
{
String name, folder;
int pos;
name = file.getParent(); //location of file
pos = name.indexOf("\\", name.indexOf("\\") + 1);
folder = name.substring(name.lastIndexOf("\\")+1,name.length());
System.out.println (""+folder);
}
Post/Questions related to Copy a Directory in Java
How to download a pdf file using java?
How to copy a file from one directory to another in java?
How to put a file into a folder in java?
How to create a directory in java?
How to read all files in a directory in java?
How to delete all files in a directory in java?
How to check if directory exists in java?
How to Download File from URL in Java
Read file to byte[] array in Java
Java File Reader Examples
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path, and Relative Path
Path toString() method in Java
Create New File in Java
In this article, we have seen Java Copy File Examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment