Skip to content

Instantly share code, notes, and snippets.

@seyedsahil
Created January 30, 2021 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyedsahil/dab2612fe5ba4970130156b31a7aab2f to your computer and use it in GitHub Desktop.
Save seyedsahil/dab2612fe5ba4970130156b31a7aab2f to your computer and use it in GitHub Desktop.
package org.sydlabz;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
* @author Seyed Sahil
*/
public class Main {
public static void main(String[] args) throws IOException {
String sourceName = "source", targetName = "target", oldPackagesName = "older";
String packageName;
// Copy package1.txt from source to target
packageName = "package1.txt";
Files.copy(Paths.get(sourceName + File.separator + packageName), Paths.get(targetName + File.separator + packageName),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File '" + packageName + "' copied from '" + sourceName + "' to '" + targetName + "'");
// Delete package1.txt from source
Files.delete(Paths.get(sourceName + File.separator + packageName));
System.out.println("File '" + packageName + "' deleted from '" + sourceName + "'");
// Move package2.txt from target to older (creates older directory befre move)
Files.createDirectory(Paths.get(targetName + File.separator + oldPackagesName));
System.out.println("Directory '" + oldPackagesName + "' created in '" + targetName + "'");
packageName = "package2.txt";
Files.move(Paths.get(targetName + File.separator + packageName),
Paths.get(targetName + File.separator + oldPackagesName + File.separator + packageName), StandardCopyOption.REPLACE_EXISTING);
System.out.println("File '" + packageName + "' moved from '" + targetName + "' to '" + targetName + File.separator + oldPackagesName +
"'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment