How to obtain the physical path of a file in java

Source: Internet
Author: User

Add a configuration file to the javaweb project to store database configuration parameters and other custom parameters. You can write a configuration file by yourself **. properties to write the custom configuration information required by the project into the file in the form of name-value pairs. Create a new conf folder under the WebRoot \ WEB-INF \ classes directory under the project directory to store the configuration file, and then put **. the properties configuration file is put in the conf file. during deployment, the IDE automatically copies the file to the corresponding path. Java web project does not enable the Service (whether it is deployed to tomcat or not) to use this. getClass (). get the Url object by getResource ("/") and print it as follows: file:/D: /myEclipseWorkspace/your project name/WebRoot/WEB-INF/classes/This path is the java web project Source Path. After the java web project deployed to tomcat is enabled, use this. getClass (). get the Url object by getResource ("/") and print it as follows: file:/D: /program/apache-tomcat-6.0.14/webapps/your project name/WEB-INF/classes/then call toString (). substring (6) to obtain the first half of the absolute path, plus the relative path, such as conf/jdbc. properties is spliced into the absolute path of the configuration file: (the "/" at the beginning of the path does not know what it means, but the test shows that "/" can be successful)/F: /D:/program/apache-tomcat-6.0.14/webapps/your project name/WEB-INF/classes/conf/send. properties java application project use this. getClass (). getReso Urce ("/") to get the Url object, print as follows: file:/D: /myEclipseWorkspace/your project name/bin/This path is the root path of the class file of the java application project. Note: When the tomcat web service is not enabled, the above path is not printed, but the classes path of the source file of your project. The above is a solution to the path problem I recently encountered when studying javaWeb. The following is a sharing of the information collected on the Internet. The paths used in Java are not tested. There are two types: absolute path and relative path. In essence, Java can only use absolute paths to find resources. All relative paths are just some convenient methods to find resources. However, the API helps us build an absolute path at the underlying level to find resources! When developing Web applications, you often need to obtain the physical path of the current WebRoot on the server. For Servlet, Action, Controller, Filter, Listener, interceptor, and other related classes, we only need to obtain ServletContext and then use ServletContext. getRealPath ("/") to obtain the physical address of the current application on the server. If ServletContext is not obtained in the class, there are two ways to do this: 1) using Java's class loading mechanism: Calling XXX. class. getClassLoader (). getResource (""); method to obtain the ClassPath, and then obtain the WebRoot directory. This method takes effect only when the class is in WebRoot/WEB-INF/classes and fails if the class is packaged into a jar file. In this case, we should use the following method. 2) spring framework ideas in WEB-INF/web. in xml, create a webAppRootKey param and specify a value (default: webapp. root) as the key value, and then use Listener, Filter, or Servlet to execute String webAppRootKey = getServletContext (). getRealPath. the root serves as the Key and the Value is written to the System Properties. Then, use System. getProperty ("webapp. root") in the program to obtain the physical path of WebRoot. Based on the second idea, we can expand it. However, for applications deployed on a server, if you do not need them yet, please read them further. The following are some methods to obtain the absolute path of classpath and the current class. You can use some of these methods to obtain the absolute path of the required resources: 1. debitNoteAction. class. getResource ("") returns the current FileTest class. the URI Directory of the class file. Not yourself! For example: file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/atacarnet/src/com/evi/modules/atacarnet/action/2. debitNoteAction. class. getResource ("/") returns the absolute URI path of the current classpath. For example: file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/3. thread. currentThread (). getContextClassLoader (). getResource ("") is also the absolute URI path of the current ClassPath, such as: file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/is recommended to get this method. 4. DebitNoteAction. class. getClassLoader (). getResource ("") or ClassLoader. getSystemResource ("") obtain the absolute URI path of the current ClassPath. Such as: file:/D:/eclipse/springTest/WebRoot/WEB-INF/classes/5. obtain the relative server path System. getProperty ("user. dir ") E: \ apache-tomcat-5.5.16 \ apache-tomcat-5.5.16 \ bin 6. to obtain the absolute path of a project, the request is generally used. getRealPath ("/") or request. getRealPath ("/config/") does not recommend request. getRealPath ("/"). You can try ServletContext. getRealPath ("/") method to obtain the absolute path of the root directory of the Web application. It is very easy to obtain src files, because src is the default relative directory. For example, you want to obtain the test of the com directory under src. java File. All you need to do is File f = new File (com/test. java); but if I want to retrieve files not in the src or WebRoot directory, I want to retrieve them from directories at the same level as src or WebRoot, for example, doc. My hard method is implemented as follows: String path = this. getServletContext (). getRealPath ("/"); Properties p = new Properties (); p. load (new FileInputStream (new File (path. substring (0, (path. lastIndexOf ("\ WebRoot") + 1) + "doc/db. properties "); System. out. println (p. getProperty ("driverName"); ----------------------------- difference of getContextPath, getServletPath, getRequestURI, getRequestURL, and getRealPath in Request assume that your web application name is news, in your browser, enter the Request Path: http: // localhost: 8080/news/main/list. jsp executes the line-oriented code and prints the following results: 1. System. out. println (request. getContextPath (); printed result:/news 2, System. out. println (request. getServletPath (); print the result:/main/list. jsp 3, System. out. println (request. getRequestURI (); printed result:/news/main/list. jsp 4, System. out. println (request. getRequestURL (); printed result: http: // localhost: 8080/news/main/list. jsp 5, System. out. println (request. getRealPath ("/"); printed result: F: \ Tomcat 6.0 \ webapps \ news \ tes

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.