March 18th, 2003, 05:20 PM
File I/O from servlet
My servlet calls another java class that reads configuration information from an external xml file and returns it to the servlet. This other class resides in a subdirectory of my web application. I do not want to include this other xml file's data in the web.xml,
AND I DON'T WANT TO HARD-CODE THE PATH to the xml file, but unless I do, my app can't find it.
My class contains the line
protected static final String configXMLFileName = "myConfig.xml";
later code opens the file and parses it.
If I put the complete path in this variable, it works. Where can I drop this file into a 'default' directory where the app will be able to read it, without any path info in the filename? I've tried web-inf, the subdirectory where my class lives, 'C:\Program Files\Apache Group\Tomcat 4.1\bin', all to no avail. No matter what, I get:
java.io.FileNotFoundException: myConfig.xml (The system cannot find the file specified)
Thanks in advance for your help.
March 19th, 2003, 09:32 AM
If the class you are using resides in a folder located in the WEB-INF/bin folder, then you can put the xml file in the root of the bin folder and access it using the "/myConfig.xml" path. If you want, you can put the xml file in the folder the actual calss resides in and then you can use the file name without any path information.
For example, if the class is in a package called com.mystuff.myclass, then you could put the xml file in WEB-INF/bin/com/mystuff and access it simply by using the myConfig.xml file name in the FileInputStream or whatever.
March 19th, 2003, 10:55 AM
Thanks for your help.
All of my servlet application classes reside under WEB-INF/classes. I have tried putting the xml file in the same folder as the class, and refering to it in code as "/myConfig.xml" and "myConfig.xml" (with and without the slash), all to no avail.
The only thing I've found to work is to drop the file into the launch directory of Tomcat, "C:\Documents and Settings\Administrator\Start Menu\Programs\Apache Tomcat 4.1". But that makes the file global to the webserver, not what I want. I want the file to be global only to a servlet context / web application.
What have I missed? Is there some special significance to WEB-INF/bin? I haven't heard of that before. Where can I learn more?
March 19th, 2003, 11:06 AM
Sorry, I meant "classes", not "bin". I was having a mental lapse.
I don't know why you can't find the file if you have put it in the same folder as the class file and referred to it by name. This works for me in my application, but I am not using Tomcat. I have Websphere 5.0.
Last edited by Nemi; March 19th, 2003 at 11:09 AM.
March 19th, 2003, 11:14 AM
I take that back. I use getClass().getResourceAsStream("/filename.xml"), and not just a FileInputStream. This is likely your problem. Using the classes Class.getResourceAsStream looks for the file relative to the location of the class file you are using. Otherwise you would have to supply a system path.
Last edited by Nemi; March 19th, 2003 at 11:25 AM.
March 19th, 2003, 12:19 PM
Whew! I got it! It works, without the leading slash, using the following line:
InputStream fis = new myConfig().getClass().getResourceAsStream ( "xmlFileName" );
The file can now reside in the same directory with the class that reads it.
Thanks for going the extra mile for me.
March 19th, 2003, 12:25 PM
You can save the creation of that object by using (I am assuming your class name is myConfig)
myConfig.class.getResourceAsStream ( "xmlFileName" );