File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Reading files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Reading files" Watch "Reading files" New topic

Reading files

manjit singh thakurratan

Joined: Dec 23, 2009
Posts: 10

I added a txt file in my package with some properties like
username = xxxx
password = xxxx
dbname = xxxx

When i start the server my servlet will open this file to get the properties.

The problem is that though the servlet and the file are in the same package, the file cannot be found.

When i write the following code

String fileName = "App.txt";
FileReader reader = new FileReader(fileName);

The reader returns a filenotfound exception

I am using eclipse and the directory the file being looked is something like this i:\eclipse\App.txt . This is the directory where I installed eclipse

It is searching for the file in the installation directory while I have placed the file in the current package.

Can some one help me

the files are as follows

Manjit Singh
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33117

The classpath of Tomcat (or whatever you are using to run your servlet) wouldn't start from wherever the servlet is. It could start from WEB-INF/classes. I usually use getResourceAsStream() and pass the package name of the file to it.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
manjit singh thakurratan

Joined: Dec 23, 2009
Posts: 10
Hi could you please tell me more in detail.....!

I cud not understand with just a method name!
I agree. Here's the link:
subject: Reading files
jQuery in Action, 3rd edition