Make sure to subscribe to our newsletter and be the first to know the news.
Make sure to subscribe to our newsletter and be the first to know the news.
In this tutorial we are going to learn how to copy a file from one location to another using Java NIO classes of Path and Files, if you are not aware of the methods available with these classes click here.
Steps are very Simple.
Create a Path Object pointing to the file and then use the copy() of Files and pass the source and destination path.
import java.nio.file.*;
import java.util.*;
public class CopyFile
{
public static void main(String[] args)
{
try
{
Path sourcePath=Paths.get("data.txt");
Path destinationPath=Paths.get("destination\\dataPasted.txt");
Files.copy(sourcePath, destinationPath);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}