Possible duplicate:
Java resource as file
I'm kind of new to Java, and I'm trying to get a text file inside a jar file.
At that moment, when I execute my jar, I should have my text file in the same folder as jar fil. If there is no text file, I will get a NullPointerException
, which I want to avoid.
What I want to do is get the txt file inside the jar, so I won't have this problem. I tried a few guides, but they didn't seem to work. My current read function is as follows:
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; }
source share