Aniket
Aniket

Reputation: 2214

Relative path File not found exception

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.

enter image description here

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

Answers (2)

Qwerky
Qwerky

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

Max Adamek
Max Adamek

Reputation: 71

try to use "\\" instead of "/"

Upvotes: -4

Related Questions