我是一種新的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;
}