tomcat project need to read file
i have a tomcat project
and have a package called com.blah.bus
under that package i have a java class, NOT A SERVLET, file that connects to a database
I want to make that java class read a con.xml file in the tomcat project
i put that file in the same directory as the class, but it didn't read....
/*java.io.File file = new java.io.File("con.xml");
if (file.exists()) {
System.out.println("!!!!!!!! !!!!!can read the file");
} else {
System.out.println("!!!!!!!! Nope");
}*/
when i tried to print out the directory files, it give me all these jar that tomcat uses, like a server directory
my question is that how do i read the con.xml file? where should i put it? thanks
[782 byte] By [
khan2265a] at [2007-11-26 13:12:51]

# 6
The getResource() method will look in the project classpath for the file. Since you specify no subdirectories the file needs to be in the top of the classpath somewhere. That would be the WEB-INF/classes folder of your project, store the xml file there. Or a better solution would be to create a directory WEB-INF/classes/res and then opening the file as follows:
java.net.URL url = getClass().getClassLoader().getResource("res/con.xml");
That should do it.
# 7
> i put that file in the same directory as the class,
If you read this xml file only in a standalone java class,you can put this in the same directory with class,and you can get it by:
public class Test{
void readXml(){
URL xmlUrl=Test.class.getResource("con.xml");
...
}
}
If you read this xml file within jee container enviroment,you can get the context path by HttpServletRequest object.so you can put the configuration file(con.xml) in the home directory of web module or any other customized directories,even WEB-INF folder.
Regards,
mauvespan