Java IO Tutorial - Java Files.copy(Path source, Path target, CopyOption ... options)








Syntax

Files.copy(Path source, Path target, CopyOption ... options) has the following syntax.

public static Path copy(Path source,  Path target,  CopyOption ... options)   throws IOException

Example

In the following code shows how to use Files.copy(Path source, Path target, CopyOption ... options) method.

// w  w w .j av a  2s.  c  o m
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

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

public class Main {

  public static void main(String[] args) {
    Path copy_from_1 = Paths.get("C:/tutorial/Java/JavaFX", "tutor.txt");

    Path copy_to_1 = Paths.get("C:/tutorial/Java/USOpen", copy_from_1
        .getFileName().toString());
    try {
      Files.copy(copy_from_1, copy_to_1, REPLACE_EXISTING, COPY_ATTRIBUTES,
          NOFOLLOW_LINKS);
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}