This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Open File Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Open File Question?" Watch "Open File Question?" New topic
Author

Open File Question?

Jackson Yen
Greenhorn

Joined: Oct 04, 2002
Posts: 5
Hi All,
I would like to opening a file at a servlet. I just put the file name in the "FileInputStream" constructor and doesn't specify the path.
ex: FileInputStream in = new FileInputStream("test.cfg");
I use Tomcat as my AP server. Where should I put the config file when I want to open a file without specifying its path? Thanks.
ps. I use "ROOT" as my webapp location
Best Regards
Jackson
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
You should never open a file this way in a servlet. The reason being that it depends on the JVM's "current" directory. You have absolutely no control over the current directory from the servlet.
Much better to provide a complete path in the servlet initialization parameters. That gives you real flexibility to put test.cfg anywhere.
Bill
Jackson Yen
Greenhorn

Joined: Oct 04, 2002
Posts: 5
Thanks, William. But the servlet was written by Vendor. I am responsible for the deployment. As you mentioned in the above, where to put the file depends on JVM's current directory. If I put the config file to the same directory where the servlet was and then make a jar file, could the servelt find the config file? If not, the last way I can do is to ask Vendor to change the code.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Well, I can't say as I think much of your vendor if they have not provided for some way to set the absolute path, or to use a location relative to the web application for your configuration file.
This is a very well known problem so they should have provided something.
Bill
Ct Arrington
Author
Greenhorn

Joined: Jan 17, 2001
Posts: 27
Hi Jackson
Here is a crude way to figure it out:
Use FileOutputStream to write a test file from your test servlet. Then find the file within the filesystem.
If you can't modify the source code to make their servlet use a parameter to find the file, then I don't see any other way. Shame on the vendor!
Hope it helps
CT


Blog | Getting Started in Software Development
Jackson Yen
Greenhorn

Joined: Oct 04, 2002
Posts: 5
Thansk CI. I found where the file was. It is on my personal folder. The platform I use is Win2k. I should change "start in" locaton to the where I want ot put it.
 
wood burning stoves
 
subject: Open File Question?
 
Similar Threads
Opening Image from Uploaded file
Best way to open a properties file
file locations in Netbeans project and in glassfish
File path \\ //
Getting Resources