I have created a Dynamic Web project in Eclipse with Tomcat.
I need to access some file resources, in this case some .txt files.
The web project is running fine, I can run it with localhost:8080
.
My folder structure looks like this:
[workspace]
- [sentiment-web]
|- [src]
| |- [sentiment]
| | - Servlet.java
| |- [sentiment.backend]
| - Handler.java
|- [WebContent]
| |- [META-INF]
| | '- MANIFEST.MF
| |- [WEB-INF]
| | |- [lib]
| | '- web.xml
| '- index.html
|- .classpath
'- .project
I make request to Servlet which redirects to Handler.java just fine.
In Handler.java I want access a file test.txt
.
Handler.java
BufferedReader br = null;
try {
File file = new File("test.txt");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
} catch (IOException e) {
e.printStackTrace();
}
I always get a FileNotFoundException
.
I tried to place the test.txt everywhere:
- workspace/sentiment-web/
- workspace/sentiment-web/WEB-CONTENT
- workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sentiment-web
- ...
Before, as a normal Java Project, the file resources were located in the project folder and it worked but now this seems not the case anymore.
Where to put file resources in a Tomcat Dynamic Web project so I can access the file resources FROM Handler.java (not from Servlet.java)?
P.S. I do not want to use absolutePath like (C:/Users/username
/workspace/sentiment-web/test.txt) because it could make trouble with portability.
EDIT:
If I place the test.txt
in the eclipse folder, it works. WHy that?
How to change that so file resources can be easily accessed without portability problems later? It should work in Eclipse but also as WAR file later in / webapps / ?