Java Files copy files with option

Introduction

The three enumeration values for StandardCopyOption are listed in the following table:

Value Meaning
ATOMIC_MOVE Perform the copy operation atomically
COPY_ATTRIBUTESCopy the source file attributes to the destination file
REPLACE_EXISTINGReplace the existing file if it already exists
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 va2  s .  co  m

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

   }

}



PreviousNext

Related