我在 JAR 文件中有一个文件。例如,它是 1.txt
。
如何访问它?我的源代码是:
Double result=0.0;
File file = new File("1.txt")); //how get this file from a jar file
BufferedReader input = new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
if(me==Integer.parseInt(line.split(":")[0])){
result= parseDouble(line.split(":")[1]);
}
}
input.close();
return result;
最佳答案
如果你的 jar 在类路径中:
InputStream is = YourClass.class.getResourceAsStream("1.txt");
如果它不在类路径上,那么您可以通过以下方式访问它:
URL url = new URL("jar:file:/absolute/location/of/yourJar.jar!/1.txt");
InputStream is = url.openStream();
关于java - 如何从 jar 文件中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2271926/