我有用于获取图像的loadImage
函数。当我试图从getFile()
函数中获取文件时,它才起作用。有没有办法使用ClassLoader和getClass()方法从res文件夹中获取文件
public BufferedImage loadImage(String img) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File("res/" + img));
} catch (Exception e1) {
try {
bimg = ImageIO.read(ClassLoader.getSystemResource("res/" + img));
} catch (Exception e2) {
try {
bimg = ImageIO.read(getClass().getResource("res/" + img));
} catch (Exception e3) {
System.out.println("Cannot load image: " + img);
}
}
}
return bimg;
}
我曾尝试使用下面的函数尝试:
public File getFile(String file) {
File f = null;
try {
f = new File("res/" + file);
} catch (Exception e1) {
try {
f = new File(ClassLoader.getSystemResource("res/" + file).getFile());
} catch (Exception e2) {
try {
f = new File(getClass().getResource("res/" + file).getFile());
} catch (Exception e3) {
System.out.println("Cannot load File: " + file);
}
}
}
return f;
}
这些功能都在一个罐子lib和我的其他项目想从罐子LIB文件。
这是在我的项目试图从罐子里的lib
File file = files.getFile("config.cfg");
我已经打开了瓶子和检查,如果该文件的jar内部存在的config.cfg获取文件。
可怕异常捕获 – m0skit0
当我把它给了我这个文件的路径:/ C:/用户/ 8803286/Dropbox/Workspace/Eclipse /程序/ Sinx%20Core/build/jar/SinxCore.jar!/res/config.cfg – 8803286