Reputation: 2214
I am working on a simple web application in NetBeans where I am getting FileNotFoundException. I have stored files in class path, so I need to use relative paths. When I tried with absolute path, it worked fine for me.
Below image shows my file system hierarchy.
I need to write content data in file DBList.txt.
My code is:
File file = new File("data/application/DBList.txt");
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
I have searched lot but not getting solution for reading file using relative path.
Upvotes: 1
Views: 5440
Reputation: 18455
The path is relative to the working directory of the server, not your project in NetBeans. Given your FNFE I suspect that the directory structure data/application/
doesn't exist under the working directory.
What server are you running and how are you starting it? You can figure out the server's working directory by logging;
File wd = new File(".");
log.debug("working dir: " + wd.getAbsolutePath());
Edit:
The File
class and the classpath are totally unrelated concepts. Don't confuse the two. If you are looking to use classpath resources have a look at the getResource()
method in ClassLoader
.
Upvotes: 5