首页javafile_copyJava I/O - 如何使用java.nio.file.Files复制文件

Java I/O - 如何使用java.nio.file.Files复制文件

我们想知道如何使用java.nio.file.Files复制文件。
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class Main {

  public static void main(String[] args) throws Exception {
    Path newFile = FileSystems.getDefault().getPath("C:/home/docs/newFile.txt");
    Path copiedFile = FileSystems.getDefault().getPath(
        "C:/home/docs/copiedFile.txt");

    Files.createFile(newFile);
    System.out.println("File created successfully!");
    Files.copy(newFile, copiedFile);
    System.out.println("File copied successfully!");
    Files.copy(newFile, copiedFile, StandardCopyOption.REPLACE_EXISTING);

  }
}