zxi
zxi

Reputation: 195

How can I find the path of a user-defined configure file (xml) in a web project of Java

I add a file under "webapp",but how can I access it? The server is tomcat and Spring framework as framework.

Thanks in advance.

Here is some code and output:

System.out.println("dddddddddddddddddddddddddddd");
//System.out.println(path.getPath());
InputStream is = getClass().getResourceAsStream("/WEB-INF/GroupWebService.xml");
if (null == is ){
    System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeee");
}

And the output is

log4j:WARN Please initialize the log4j system properly.
dddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeee
Mar 15, 2012 1:55:52 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'application'

Upvotes: 0

Views: 74

Answers (2)

Rahul Borkar
Rahul Borkar

Reputation: 2762

You can use

 File directory = new File (".");
 String path = "";
 try {   
        path = directory.getAbsolutePath()
        System.out.println ("Current directory's absolute  path: "+ directory.getAbsolutePath());
 }catch(Exception e) {
     System.out.println("Exceptione is ="+e.getMessage());
 }

Which will probably print path of your project directory and then you can use following statement to traverse to your WEB-INF

if(!path.equals("")){
  path += File.seperator + "WEB-INF" + File.seperator + "filename.xml";
}

Upvotes: 2

coollzh
coollzh

Reputation: 23

    System.out.println("dddddddddddddddddddddddddddd");
    //System.out.println(path.getPath());
    InputStream is = getClass().getResourceAsStream("/WEB-INF/util.tld");
    if (null == is ){
        System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeee");
    }else
    {
        System.out.println(is.getClass().getName());
    }

Upvotes: 1

Related Questions