我是一种新的Java和我试图让里面的文本文件Jar文件。
当我执行我的jar时,我必须将我的文本文件放在与jar fil相同的文件夹中。如果文本文件不在那里,我会得到一个NullPointerException
,这是我想避免的。
我想要做的是获取罐子里面的txt文件,所以我不会有这个问题。我试了一些指南,但他们似乎没有工作。我目前的阅读功能是这样的:
public static HashSet<String> readDictionary()
{
HashSet<String> toRet = new HashSet<>();
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Dictionary.txt");
try (DataInputStream in = new DataInputStream(fstream)) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Read Lines
toRet.add(strLine);
}
}
return toRet;
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return null;
}