将 org.jackhuang.hmcl.ui 从 java.io.File 迁移至 NIO (#4501)
This commit is contained in:
@@ -26,10 +26,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.ZonedDateTime;
|
||||
@@ -50,6 +47,28 @@ public final class FileUtils {
|
||||
private FileUtils() {
|
||||
}
|
||||
|
||||
public static @Nullable Path toPath(@Nullable File file) {
|
||||
try {
|
||||
return file != null ? file.toPath() : null;
|
||||
} catch (InvalidPathException e) {
|
||||
LOG.warning("Invalid path: " + file);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable List<Path> toPaths(@Nullable List<File> files) {
|
||||
if (files == null) return null;
|
||||
return files.stream().map(FileUtils::toPath).filter(Objects::nonNull).toList();
|
||||
}
|
||||
|
||||
public static @Nullable File toFile(@Nullable Path file) {
|
||||
try {
|
||||
return file != null ? file.toFile() : null;
|
||||
} catch (UnsupportedOperationException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canCreateDirectory(String path) {
|
||||
try {
|
||||
return canCreateDirectory(Paths.get(path));
|
||||
|
||||
Reference in New Issue
Block a user