title: Including a text file inside a jar file and reading it
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/676097/java-resource-as-file">Java resource as file</a>  </p>
  </blockquote>
  
  
  
  <p>I am kind of new to Java and I am trying to get a text file inside a Jar file. </p>
  
  <p>At the moment when I execute my jar I have to have my text file in the same folder as the jar fil.  If the text file is not there I'll get a <code>NullPointerException</code>, which I want to avoid.</p>
  
  <p>What I want to do is to get the txt file inside the jar so I wont have this problem.  I tried some guides but they didn't seem to work.  My current read function goes like this:</p>
  
  <pre><code>public static HashSet&lt;String&gt; readDictionary()
  {
      HashSet&lt;String&gt; toRet = new HashSet&lt;&gt;();
       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;
  }
  </code></pre>