import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.apache.commons.io.FileUtils;

public class CopyTest {
    public static void main(String[] args) throws Exception {
        File src = new File(args[1]);
        if (!src.exists()) {
            System.out.println("Source file does not exist: " + src.getName());
            System.exit(-1);
        }
        File dest = new File(args[2]);
        try {
            System.out.println("Trying Java NIO mechanism...\n");
            System.out.println("copy".equalsIgnoreCase(args[0])?"Copying":"Moving" + " file...\n\tSource file: " + args[1] + "\n\tDestination file: " + args[2]);
            if ("copy".equalsIgnoreCase(args[0])) {
                Files.move(src.toPath(), dest.toPath(), StandardCopyOption.ATOMIC_MOVE);
                if (src.exists()) {
                    System.out.println("FAILED: Source file still exists: " + src.getName());
                    System.exit(-1);
                } else {
                    System.out.println("SUCCESS: Source file no longer exists in source: " + src.getName());
                }
            } else {
                Files.copy(src.toPath(), dest.toPath());
            }
        } catch (Throwable t) {
            System.out.println("\nJava NIO mechanism FAILED.\n");
            t.printStackTrace();
        }
        System.out.println("\n\nTrying Apache FileUtils mechanism...\n");
        System.out.println("copy".equalsIgnoreCase(args[0])?"Copying":"Moving" + " file...\n\tSource file: " + args[1] + "\n\tDestination file: " + args[2]);
        if ("move".equalsIgnoreCase(args[0])) {
            FileUtils.moveFile(src, dest);
            if (src.exists()) {
                System.out.println("FAILED: Source file still exists: " + src.getName());
                System.exit(-1);
            } else {
                System.out.println("SUCCESS: Source file no longer exists in source: " + src.getName());
            }
        } else {
            FileUtils.copyFile(src, dest);
        }

    }
}
