On this section of my series of java tutorial, we will be concentrating on topic java nio file copy. The java nio packages is recently added in Java 7. So make sure in running the example below you must have the latest java packages. I have recently getting email request for me to write articles regarding the java nio package, thus will be the first article of my series of tutorials in java nio.

Let’s take a look on my previous article on how to copy a file using java io package. If you would notice the example source code provided is a little bit long and confusing because you have to use the FileInputStream and the FileoutputStream. The essence of the mentioned article is to read the stream from the source file and write as a stream to the target file. This solution is a little bit slow compared to using only the java nio packages.

As we move along on the article, we will discover some interesting and useful functionality of java nio. These options are not yet available from previous version, thus I could say that working with files now in java is a little bit easier and more productive.

JAVA NIO file copy – single file

This source code shows a basic example on how to copy a file using java nio. One notable option that we have used is the usage of the static variable REPLACE_EXISTING of StandardCopyOption class of java.nio.file package. As the name of the variable suggests, this option will dictate that the target file will be replaced if it is already existing.

package com.javatutorialhq.tutorial;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

//java nio file copy example

public class JavaNIOFileCopy {

	public static void main(String[] args) {
		Path source = Paths.get("E:/tmp/java/tutorial/nio/file/copy/source/sample text.txt");
		Path target = Paths.get("E:/tmp/java/tutorial/nio/file/copy/target/sample output.txt");
		try {
			//replace existing file using java nio package
			System.out.println(Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

Sample Output:

E:\tmp\java\tutorial\nio\file\copy\target\sample output.txt

We have used on the example above the Files.copy(Path source, Path target , CopyOption option) method of java.nio package. The output of this method is the target filename which is the same as that of specified target path.

From the example above we have used the option REPLACE_EXISTING. There are two other possible options for this, COPY_ATTRIBUTES and ATOMIC_MOVE. I am gonna leave to you to discover the usage of those two attributes.

Java NIO file copy : All files in a folder

As you would already notice from previous example, copying a file is so simple now compared to the old method of copying file using java io packages. However copying of folder would not be as simple as that of a single copy. Yes we can just put the folder name instead of the specific file to be copied, but if you will do this the result would only be a folder without the contains.[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]