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.
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 yourtomcatdirectory.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.