gpt4 book ai didi

java - 如何从 jar 文件中读取文件?

转载 作者:IT老高 更新时间:2023-10-28 21:07:00 38 4
gpt4 key购买 nike

我在 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com