52 lines
1.5 KiB
Java
52 lines
1.5 KiB
Java
package ctbrec.io;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
|
|
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() {}
|
|
|
|
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) {
|
|
if (parent.equals(recDir)) {
|
|
return;
|
|
}
|
|
LOG.debug("Deleting empty directory {}", parent.getAbsolutePath());
|
|
Files.delete(parent.toPath());
|
|
parent = parent.getParentFile();
|
|
}
|
|
}
|
|
|
|
public static void deleteDirectory(File directory) throws IOException {
|
|
if (!directory.exists()) {
|
|
return;
|
|
}
|
|
|
|
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 (!deletedAllFiles) {
|
|
throw new IOException("Couldn't delete all files in " + directory);
|
|
}
|
|
}
|
|
}
|