Java Files copy files

Description

Java Files copy files

import java.io.IOException;
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) {
      Path newFile = FileSystems.getDefault().getPath("Main.java");
      Path copiedFile = FileSystems.getDefault().getPath("Main.txt");

      try {//from   w  w  w . j  a  v a 2  s .  c  om

         Files.copy(newFile, copiedFile);
         System.out.println("File copied successfully!");
      } catch (IOException e) {
         System.out.println("IO Exception.");
      }

   }

}
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Main {
  public static void main(String[] args) {
    Path source = Paths.get("Main.java");
    Path target = Paths.get("test1_backup.txt");

    try {/*from  w  w  w. j a va 2  s. co  m*/
      Path p = Files.copy(source, target,
          StandardCopyOption.REPLACE_EXISTING,
          StandardCopyOption.COPY_ATTRIBUTES);
      System.out.println(source + " has been copied to " + p);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related