Delete directory recursively in IoUtils
This commit is contained in:
parent
49a581446b
commit
287d0804c8
|
@ -1,26 +1,22 @@
|
||||||
package ctbrec.io;
|
package ctbrec.io;
|
||||||
|
|
||||||
|
import ctbrec.Config;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.FileVisitOption;
|
import java.nio.file.*;
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.SimpleFileVisitor;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import ctbrec.Config;
|
|
||||||
|
|
||||||
public class IoUtils {
|
public class IoUtils {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(IoUtils.class);
|
private static final Logger LOG = LoggerFactory.getLogger(IoUtils.class);
|
||||||
|
|
||||||
private IoUtils() {}
|
private IoUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
public static void deleteEmptyParents(File parent) throws IOException {
|
public static void deleteEmptyParents(File parent) throws IOException {
|
||||||
File recDir = new File(Config.getInstance().getSettings().recordingsDir);
|
File recDir = new File(Config.getInstance().getSettings().recordingsDir);
|
||||||
|
@ -44,6 +40,9 @@ public class IoUtils {
|
||||||
File[] files = directory.listFiles();
|
File[] files = directory.listFiles();
|
||||||
boolean deletedAllFiles = true;
|
boolean deletedAllFiles = true;
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
deleteDirectory(file);
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
LOG.trace("Deleting {}", file.getAbsolutePath());
|
LOG.trace("Deleting {}", file.getAbsolutePath());
|
||||||
Files.delete(file.toPath());
|
Files.delete(file.toPath());
|
||||||
|
@ -52,6 +51,7 @@ public class IoUtils {
|
||||||
LOG.debug("Couldn't delete {}", file, e);
|
LOG.debug("Couldn't delete {}", file, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!deletedAllFiles) {
|
if (!deletedAllFiles) {
|
||||||
throw new IOException("Couldn't delete all files in " + directory);
|
throw new IOException("Couldn't delete all files in " + directory);
|
||||||
|
@ -64,7 +64,7 @@ public class IoUtils {
|
||||||
final long[] size = {0};
|
final long[] size = {0};
|
||||||
int maxDepth = 7;
|
int maxDepth = 7;
|
||||||
try {
|
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
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
||||||
size[0] += attrs.size();
|
size[0] += attrs.size();
|
||||||
|
|
Loading…
Reference in New Issue