FileUtils.java 1.01 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.utils;

KingRainbow44's avatar
KingRainbow44 committed
3
4
import emu.grasscutter.Grasscutter;

Melledy's avatar
Melledy committed
5
6
7
8
9
10
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

KingRainbow44's avatar
KingRainbow44 committed
11
public final class FileUtils {
Melledy's avatar
Melledy committed
12
13
14
15
16
17
	public static void write(String dest, byte[] bytes) {
		Path path = Paths.get(dest);
		
		try {
			Files.write(path, bytes);
		} catch (IOException e) {
KingRainbow44's avatar
KingRainbow44 committed
18
			Grasscutter.getLogger().warn("Failed to write file: " + dest);
Melledy's avatar
Melledy committed
19
20
21
22
23
24
25
26
27
28
29
		}
	}
	
	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) {
KingRainbow44's avatar
KingRainbow44 committed
30
			Grasscutter.getLogger().warn("Failed to read file: " + path);
Melledy's avatar
Melledy committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		}
		
		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;
		}
	}
}