重複の可能性:
ファイルとしての Java リソース
私はJavaが初めてで、Jarファイル内のテキストファイルを取得しようとしています.
jar ファイルを実行する時点で、jar ファイルと同じフォルダーにテキスト ファイルを配置する必要があります。テキストファイルがそこにない場合、私はNullPointerException
避けたい.
私がやりたいのは、jar内の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;
}