This week's book giveaway is in the Game Development forum.
We're giving away four copies of Badass: Making Users Awesome and have Kathy Sierra on-line!
See this thread for details.
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

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
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: 31634

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]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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