Grasscutter/src/main/java/emu/grasscutter/utils/FileUtils.java

48 lines
1.0 KiB
Java
Raw Normal View History

2022-04-17 20:43:07 +08:00
package emu.grasscutter.utils;
2022-04-18 13:11:27 +08:00
import emu.grasscutter.Grasscutter;
2022-04-17 20:43:07 +08:00
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2022-04-18 13:11:27 +08:00
public final class FileUtils {
2022-04-17 20:43:07 +08:00
public static void write(String dest, byte[] bytes) {
Path path = Paths.get(dest);
try {
Files.write(path, bytes);
} catch (IOException e) {
2022-04-18 13:11:27 +08:00
Grasscutter.getLogger().warn("Failed to write file: " + dest);
2022-04-17 20:43:07 +08:00
}
}
public static byte[] read(String dest) {
return read(Paths.get(dest));
}
public static byte[] read(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
2022-04-18 13:11:27 +08:00
Grasscutter.getLogger().warn("Failed to read file: " + path);
2022-04-17 20:43:07 +08:00
}
return new byte[0];
}
public static byte[] read(File file) {
return read(file.getPath());
}
public static String getFilenameWithoutPath(String fileName) {
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
}
}