Copy File

The javaxt.io.File class makes it easy to copy files. Here are 2 methods you can use.

Copy File to a Directory

Here's an example of how to copy a file to a directory. In this example, we simply define an input file, the output directory, and execute the copyTo() method on the file.

javaxt.io.File file = new javaxt.io.File("/input/file.txt");
javaxt.io.Directory dir = new javaxt.io.File("/output/");
file.copyTo(dir, true); //true to overwrite any existing file

Copy File to Another File

Here's an example of how to copy a file to another file. In this example, we simply define an input file, the output file, and execute the copyTo() method on the input file.

javaxt.io.File source = new javaxt.io.File("/input/file.txt");
javaxt.io.File destination = new javaxt.io.File("/output/file.txt");
source.copyTo(destination, true); //true to overwrite any existing file

Buffer Size

Users have the option to set the size of the buffer used to read/write bytes with the setBufferSize() method. The default is 1MB (1,048,576 bytes).

javaxt.io.File source = new javaxt.io.File("/input/file.txt");
source.setBufferSize(8192*1024); //8MB
source.copyTo(new javaxt.io.File("/output/file.txt"), true);

Non-blocking IO

Under the hood, the copy method utilizes non-blocking IO (NIO) for fast file copies.