Java tool for obtaining paths

Source: Internet
Author: User

This problem depends on where your configuration file is stored. If you put it in the project's classes directory (or subdirectory), you can use **. class. getresource ('relative path') to obtain the configuration file path. if it is another directory, you can only get the project root directory + configuration file directory through servletcontext at project startup to determine the path. and put the path in a place where class files can be referenced.
The following is a Class I wrote when I was working on the project to get the path. It may not be well written, but I still hope it will help you:
Package com. example. Web;

Import java. Io. file;
Import java.net. url;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;

/**
* Path Retrieval Class
**/
Public class webpath {
/**
* Obtain the absolute path of the Project root directory
*
* @ Return for example: F: \ tongjianpeng \ j2eeutil
**/
Public static string getabsolutepathwithproject (){
Return System. getproperty ("user. dir ");
}

/**
* Obtain the drive letter of the project.
**/
Public static string getdriverpathwithproject (){
Return new file ("/"). getabsolutepath ();
}

/**
* Obtain the absolute path of the Project root directory
*
* @ Return project root. For example, <br/> F: \ Tomcat \ webapps \ j2eeutil \
**/
Public static string getabsolutepathwithwebproject (
Httpservletrequest request ){
Return request. getsession (). getservletcontext (). getrealpath ("/");
}

/**
* Obtain the absolute path of the specified directory under the root directory of the project.
*
* @ Param the specified directory under the root directory of the project
*. Example:/login/
* @ Return the specified directory under the root directory of the project. For example: <br/> F: \ Tomcat \ webapps \ j2eeutil \ login \
**/
Public static string getabsolutepathwithwebproject (
Httpservletrequest request, string path ){
Return request. getsession (). getservletcontext (). getrealpath (PATH );
}

/**
* Obtain the absolute path of the Project root directory
*
* @ Return project root. For example, <br/> F: \ Tomcat \ webapps \ j2eeutil \
**/
Public static string getabsolutepathwithwebproject (servletcontext context ){
Return context. getrealpath ("/");
}

/**
* Obtain the absolute path of the specified directory under the root directory of the project.
*
* @ Param the specified directory under the root directory of the project
*. Example:/login/
* @ Return the specified directory under the root directory of the project. For example: <br/> F: \ Tomcat \ webapps \ j2eeutil \ login \
**/
Public static string getabsolutepathwithwebproject (servletcontext context,
String path ){
Return context. getrealpath (PATH );
}

/**
* Obtain the absolute path of the Project classpath directory.
*
* @ Return absolute path of the classes directory <br/>
* File:/F:/tomcat/webapps/j2eeutil/WEB-INF/classes/
**/
Public static URL getabsolutepathwithclass (){
Return webpath. Class. getresource ("/");
}

/**
* Obtain the absolute path of the specified directory under the project classpath directory.
*
* @ Param path
* The specified directory under the classes directory. For example:/COM/
* @ Return file:/F:/tomcat/webapps/j2eeutil/WEB-INF/classes/COM/
**/
Public static URL getabsolutepathwithclass (string path ){
Return webpath. Class. getresource (PATH );
}

/**
* Obtain the absolute path of the directory where the specified class file is located.
*
* @ Param clazz
* Class
* @ Return indicates the absolute path of the class file. For example: <br/> main. Java class under the com. Aries. util. Web package. <br/>
* Path: file :/
* F:/tomcat/webapps/j2eeutil/WEB-INF/classes/COM/Aries/util/web/
**/
Public static URL getabsolutepathwithclass (class clazz ){
Return clazz. getresource ("");
}
}

References: http://zhidao.baidu.com/question/142445629.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.