1

I am using Intelli-J Idea IDE and working on a Java EE Web project. I am trying to read words.txt file. The code is working fine in command line, but when I deploy the code into glassfish server it gives me "FileNotFoundException".

File is located in "src/Analyser/pWords.txt"

Code to read the file content:

Scanner scan = new Scanner(new File("src/Analyser/pWords.txt"));

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 main and the resources in the src folder, the package becomes main.resources, I tried another way such as:

InputStream inputStream  = new FileInputStream("src/Analyser/pWords.txt");

AND

InputStream inputStream  = MyClass.class.getClassLoader().getResourceAsStream("src/Analyser/pWords.txt");
InputStream inputStream  = MyClass.class.getClassLoader().getResourceAsStream("pWords.txt");

But the result is null or error.

So is there any way I can read the file from src/Analyser/ directory in my project? It's a Java EE Web project.

UPDATE:

From the Project Structure > Artifacts > WEB-INF folder, I uploaded the pWords.txt file. and Now finally, BufferedReader is not empty, while debugging I can see that bufferedReader contains data. But when I copy the data to a string variable, the variable remains null. I am quite confused about it.

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) {

    } 
Nix
  • 150
  • 1
  • 13
  • 2
    You cannot use a file system in Java EE. You should be opening an input stream for a classpath resource. Should not be source. /resources should be in classpath. Read relative to the root of your package. – duffymo Dec 21 '17 at 18:36
  • @duffymo how can I add resources to the classpath? secondly, I am unable to create resources dir either. – Nix Dec 21 '17 at 18:51
  • 1
    Unable to create? Of course you can. I would recommend using the standard Maven directory structure and building your Java EE package using IntelliJ artifact. The src/main/resources contents go into CLASSPATH automatically. – duffymo Dec 21 '17 at 19:00
  • @duffymo please check the updated question. – Nix Dec 21 '17 at 19:29

2 Answers2

1

Nix try this

InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("Analyser/pWords.txt”);
  • `this` cannot be referenced from a static context. I get this error. – Nix Dec 21 '17 at 19:30
  • Can you post your entire class? –  Dec 21 '17 at 19:57
  • It's a static class, so I am unable to use `this` because it's non-static. I am just having an issue with the method, I updated the question please check. – Nix Dec 21 '17 at 20:26
1

You do have two problems/erros within your code!

1.) You are using the variable t in two ways. On the one hand you are combining the results by t += bf.readLine(); and on the other hand you are overwriting t with further input from the stream by (t = bf.readLine()) != null. Since this is the abort condition of the while loop, t has ALWAYS to be null in the end!

2.) You are calling bf.readLine() two times - in the while condition and again within the loop. If you read the API/javadoc of readLine() then you'll get to know that this two calls will return 2 different(!) results. This way, if the stream (bf) only contains one line of text, then in your condition the call of bf.readLine() will return your line of text and when you try to add it to the variable t then your (second) call of bf.readLine() will return null, since the end of the stream has been reached.

Possible solution:

String singleLine;
String wholeText = "";
try {
    while((singleLine = bf.readLine()) != null) {
        wholeText += singleLine;
    }
} catch(IOException x) {
} 
Niklas P
  • 3,052
  • 2
  • 13
  • 16