1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2025-09-06 11:06:58 +00:00
Files
PermissionsTime/src/main/gg/frog/mc/permissionstime/utils/FileUtil.java
2017-07-31 01:45:11 +08:00

65 lines
1.6 KiB
Java

package gg.frog.mc.permissionstime.utils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class FileUtil {
public interface FindFilesDo {
boolean isProcess(String fileName);
void process(String fileName, InputStream is);
}
public static void findFilesFromJar(FindFilesDo ffd, Class<?> jarClazz) {
JarFile jarFile = null;
try {
String jarFilePath = jarClazz.getProtectionDomain().getCodeSource().getLocation().getFile();
jarFilePath = URLDecoder.decode(jarFilePath, Charset.defaultCharset().name());
jarFile = new JarFile(jarFilePath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (ffd.isProcess(e.getName())) {
InputStream is = jarFile.getInputStream(e);
ffd.process(e.getName(), is);
try {
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void writeOnFile(String fileName, String content) {
FileWriter fw;
try {
File f = new File(fileName);
fw = new FileWriter(f, true);
fw.write(content + "\r\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}