title: Read a file in a Java EE Web Project
body: <p>I am using Intelli-J Idea IDE and working on a Java EE Web project. I am trying to read <code>words.txt</code> file. The code is working fine in command line, but when I deploy the code into glassfish server it gives me "FileNotFoundException". </p>
  
  <p>File is located in "src/Analyser/pWords.txt"</p>
  
  <p>Code to read the file content:</p>
  
  <pre><code>Scanner scan = new Scanner(new File("src/Analyser/pWords.txt"));
  </code></pre>
  
  <p>After searching StackOverflow, I found out that I need to add this file to resources directory. Unfortunately, I am unable to create resources. When I create the package <code>main</code> and the resources in the <code>src</code> folder, the package becomes <code>main.resources</code>, I tried another way such as:</p>
  
  <pre><code>InputStream inputStream  = new FileInputStream("src/Analyser/pWords.txt");
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>InputStream inputStream  = MyClass.class.getClassLoader().getResourceAsStream("src/Analyser/pWords.txt");
  InputStream inputStream  = MyClass.class.getClassLoader().getResourceAsStream("pWords.txt");
  </code></pre>
  
  <p>But the result is <code>null</code> or <code>error</code>.</p>
  
  <p>So is there any way I can read the file from <code>src/Analyser/</code> directory in my project? It's a Java EE Web project.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>From the Project Structure > Artifacts > <code>WEB-INF</code> folder, I uploaded the <code>pWords.txt</code> file. and Now finally, BufferedReader is not empty, while debugging I can see that <code>bufferedReader</code> contains data. But when I copy the data to a string variable, the variable remains null. I am quite confused about it.</p>
  
  <p>Code:</p>
  
  <pre><code>ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      InputStream stream = classLoader.getResourceAsStream("pWords.txt");
      BufferedReader bf = new BufferedReader(new InputStreamReader(stream));
      String t = "";
      try {
          while((t = bf.readLine()) != null) { // in debugging, (cb) contains the char data as char elements.
              t += bf.readLine();  // but t = null
          }
      } catch(IOException x) {
  
      } 
  </code></pre>