0

I have a Web Project where I am using the following code:

try {
    br1 = new BufferedReader(new FileReader("queryWords.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Now for this, it is throwing me the following exception:

java.io.FileNotFoundException: queryWords.txt (The system cannot find the file specified)

(I am storing 'queryWords.txt' file in the Project Folder itself)

When I change my code to the following:

br1 = new BufferedReader(new FileReader("C://Users//Project//Demo//queryWords.txt"));

where I have given the full absolute path, it runs perfectly but when I change my project to a plane 'Java Project', the initial code works perfectly fine and I am not getting any exception.

So why this unexpected behavior is happening? When my project is just 'Java Project', I don't have to provide the full path but when I have the 'Dynamic Web Project', I am getting the error if I don't give the full absolute path of the file.

Note:I am using Eclipse IDE.

andyp
  • 6,229
  • 3
  • 38
  • 55
Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37
  • Find out which path it is running from. It's probably an unexpected one – keyser Apr 20 '14 at 13:25
  • Try to add a `/` before the file name `new FileReader("/queryWords.txt")` – Vinz243 Apr 20 '14 at 13:25
  • @Vinz243: no, it's not working.. – Shrikant Kakani Apr 20 '14 at 13:28
  • is your java project a maven project – vikeng21 Apr 20 '14 at 13:30
  • 1
    Try `System.out.println(new File("").getAbsolutePath());` to see the default directory. – indivisible Apr 20 '14 at 13:30
  • *Because that's how web projects work*. They are intended to run within an app-server, *without any knowledge of the filesystem*. You need to load any files off the classpath, not by trying to open them as a file. I suggest starting with the [J2EE Tutorial](http://docs.oracle.com/javaee/1.4/tutorial/doc/WebApp.html#wp76431), and pay particular attention to the `WEB-INF` directory. – kdgregory Apr 20 '14 at 13:30
  • @vikeng21: no. its a simple server application. Although I am also using JQuery (which I don't think is an issue) – Shrikant Kakani Apr 20 '14 at 13:31
  • @mbs: well yes, I am getting some other path with System.out.println(new File("queryWords.txt").getAbsolutePath()); But then, if I have to write that whole path in my application, then how I am suppose to submit the code? I mean it is a coursework project, and my professor won't make all these changes and he will just run the application assuming that we will handle all these things by ourselves.. – Shrikant Kakani Apr 20 '14 at 13:34

1 Answers1

2

"When my project is just 'Java Project' I don't have to provide the full path"

Your IDE is reading the file from working directory which is normally the project root.

"when I have the 'Dynamic Web Project', I am getting the error if I don't give the full absolute path of the file"

Web application are packaged in to a .war(.jar). Once the application is built, the files in the src are included into the class path, and should be read as such. In your case, your file was never included into the jar, as it's not a resource within the src. Even though reading the file from the file system might work on your system, no one else using the application will be able to read it.

Instead you should have your resources in the src, say a resources package. It depends on the structure of your build, what path you should provide, but I think with a normal maven project structure (with the file in the src/main/resources/myfile.txt) and a regular java project structure (src/resources/myfile.txt) you are safe to use the following

InputStream is = getClass().getResourceAsStream("/resources/myfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720