Controlling how a file is copied - Java File Path IO

Java examples for File Path IO:File Operation

Description

Controlling how a file is copied

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Path newFile = FileSystems.getDefault().getPath("C:/home/docs/newFile.txt");
    Path copiedFile = FileSystems.getDefault().getPath(
        "C:/home/docs/copiedFile.txt");
    try {//w w  w.  j  a va2  s  .co  m
      Files.createFile(newFile);
      System.out.println("File created successfully!");
      Files.copy(newFile, copiedFile);
      System.out.println("File copied successfully!");
    } catch (IOException e) {
      System.out.println("IO Exception.");
    }
  }
}

Related Tutorials