Refactor: Add removeAndBackupFile method

This commit is contained in:
chimp1984 2020-10-01 16:46:28 -05:00
parent d27e4dc249
commit 6f6836f733
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3

View File

@ -37,14 +37,12 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
@Slf4j
public class FileUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
public static void rollingBackup(File dir, String fileName, int numMaxBackupFiles) {
if (dir.exists()) {
File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
@ -203,11 +201,24 @@ public class FileUtil {
FileUtils.copyDirectory(source, destination);
}
static File createNewFile(Path path) throws IOException {
public static File createNewFile(Path path) throws IOException {
File file = path.toFile();
if (!file.createNewFile()) {
throw new IOException("There already exists a file with path: " + path);
}
return file;
}
public static void removeAndBackupFile(File dbDir, File storageFile, String fileName, String backupFolderName)
throws IOException {
File corruptedBackupDir = new File(Paths.get(dbDir.getAbsolutePath(), backupFolderName).toString());
if (!corruptedBackupDir.exists() && !corruptedBackupDir.mkdir()) {
log.warn("make dir failed");
}
File corruptedFile = new File(Paths.get(dbDir.getAbsolutePath(), backupFolderName, fileName).toString());
if (storageFile.exists()) {
renameFile(storageFile, corruptedFile);
}
}
}