Reputation: 4681
I'm trying to read from a text file in Netbeans. In the top level of my project directory I have foo.txt. Then in my code I have:
File file = new File("foo.txt");
It throws a FileNotFoundException
, however. It's a Java web application using Spring and Tomcat, but I'm not sure if those details matter since I'm running the whole thing inside Netbeans. Basically, I just want to know where I need to put the file so Netbeans will read it.
Update - good call guys, it's looking in Tomcat's bin directory. Now this may be a stupid question but, how would I go about getting it to look in my top level project directory? I feel like dropping text files into tomcat's bin would be innapropriate.
Upvotes: 0
Views: 10527
Reputation: 48186
try System.getProperty("user.dir")
to get current working directory
Upvotes: 0
Reputation: 18601
I would use the following to figure out where to put the file:
System.out.println(System.getProperty("user.dir"));
Upvotes: 4
Reputation: 33161
To directly answer your question, If you're running an application on Tomcat, files will be opened from the current working directory. That will likely be the bin/
folder in your tomcat
directory.
You can find out for sure where your program is looking by examining the result of file.getAbsolutePath()
.
However, for web applications, I would suggest putting files you need to read in your classpath so you don't have to depend on a certain file structure when you deploy your web application.
Upvotes: 3
Reputation: 3456
You can try printing the absolute path of the File
object to see where it is looking on the filesystem.
System.out.println(file.getAbsolutePath());
Upvotes: 5