Skip to content

Instantly share code, notes, and snippets.

@lamngockhuong
Created September 14, 2018 03:37
Show Gist options
  • Save lamngockhuong/7ef85efc9a46d0d511e96154ed1f59c8 to your computer and use it in GitHub Desktop.
Save lamngockhuong/7ef85efc9a46d0d511e96154ed1f59c8 to your computer and use it in GitHub Desktop.
FileHelper
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
public class FileHelper {
public static void copyFolder(File sourceFolder, File destinationFolder) throws IOException {
if (sourceFolder.isDirectory()) {
if (!destinationFolder.exists()) {
destinationFolder.mkdir();
}
String files[] = sourceFolder.list();
for (String file : files) {
File srcFile = new File(sourceFolder, file);
File destFile = new File(destinationFolder, file);
copyFolder(srcFile, destFile);
}
} else {
Files.copy(sourceFolder.toPath(), destinationFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
public static void deleteDirectory(String directoryPath) throws IOException {
Path pathToBeDeleted = Paths.get(directoryPath);
Files.walk(pathToBeDeleted)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment