Delete directory recursively in IoUtils
This commit is contained in:
parent
49a581446b
commit
287d0804c8
|
@ -1,34 +1,30 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import ctbrec.Config;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ctbrec.Config;
|
||||
|
||||
public class IoUtils {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(IoUtils.class);
|
||||
|
||||
private IoUtils() {}
|
||||
private IoUtils() {
|
||||
}
|
||||
|
||||
public static void deleteEmptyParents(File parent) throws IOException {
|
||||
File recDir = new File(Config.getInstance().getSettings().recordingsDir);
|
||||
while (parent != null && (parent.list() != null && parent.list().length == 0 || !parent.exists()) ) {
|
||||
while (parent != null && (parent.list() != null && parent.list().length == 0 || !parent.exists())) {
|
||||
if (parent.equals(recDir)) {
|
||||
return;
|
||||
}
|
||||
if(parent.exists()) {
|
||||
if (parent.exists()) {
|
||||
LOG.debug("Deleting empty directory {}", parent.getAbsolutePath());
|
||||
Files.delete(parent.toPath());
|
||||
}
|
||||
|
@ -44,12 +40,16 @@ public class IoUtils {
|
|||
File[] files = directory.listFiles();
|
||||
boolean deletedAllFiles = true;
|
||||
for (File file : files) {
|
||||
try {
|
||||
LOG.trace("Deleting {}", file.getAbsolutePath());
|
||||
Files.delete(file.toPath());
|
||||
} catch (Exception e) {
|
||||
deletedAllFiles = false;
|
||||
LOG.debug("Couldn't delete {}", file, e);
|
||||
if (file.isDirectory()) {
|
||||
deleteDirectory(file);
|
||||
} else {
|
||||
try {
|
||||
LOG.trace("Deleting {}", file.getAbsolutePath());
|
||||
Files.delete(file.toPath());
|
||||
} catch (Exception e) {
|
||||
deletedAllFiles = false;
|
||||
LOG.debug("Couldn't delete {}", file, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,10 +61,10 @@ public class IoUtils {
|
|||
}
|
||||
|
||||
public static long getDirectorySize(File dir) {
|
||||
final long[] size = { 0 };
|
||||
final long[] size = {0};
|
||||
int maxDepth = 7;
|
||||
try {
|
||||
Files.walkFileTree(dir.toPath(), EnumSet.noneOf(FileVisitOption.class), maxDepth, new SimpleFileVisitor<Path>() {
|
||||
Files.walkFileTree(dir.toPath(), EnumSet.noneOf(FileVisitOption.class), maxDepth, new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
||||
size[0] += attrs.size();
|
||||
|
|
Loading…
Reference in New Issue