In-depth analysis of the Java file path of the detailed _java

Source: Internet
Author: User
the paths used in Java are divided into two types: absolute and relative. In the final analysis, Java essentially only uses absolute paths to find resources. All the relative paths to finding resources are just a few convenient ways. But the API at the bottom helps us build an absolute path to find resources!
When developing Web applications, it is often necessary to obtain the physical path of the current Webroot in the server.
If it's a servlet, Action, Controller, or filter, Listener, interceptor, etc, we just need to get the ServletContext and then pass Servletcontext.getrealpath (" /"To obtain the physical address currently applied to the server.
If you don't get ServletContext in a class, there are two ways to do it:
1. Use Java class loading mechanism to invoke XXX.class.getClassLoader (). GetResource (""); method to get to the classpath and then process the Webroot directory, which can only take effect under webroot/web-inf/classes, and if the class is packaged into a jar file, the method fails. This should be done in one of the following ways.
2. Spring framework thinking, in Web-inf/web.xml, create a webapprootkey param, specify a value (default to Webapp.root) as the key value, and then through listener, or filter, Or the servlet executes string webapprootkey = Getservletcontext (). Getrealpath ("/"); and the corresponding webapp.root of Webapprootkey as key, and value is written into system properties. The physical path of the Webroot is then obtained by System.getproperty ("Webapp.root") in the program.
According to the second idea, we can expand it. But for applications that are deployed on a single server, look down if it's not what you need.
Here are some ways to get classpath and the absolute path of the current class. You can use some of these methods to get the absolute path to the resources you need:
1. DebitNoteAction.class.getResource ("")
Gets the URI directory of the current class Filetest.class file. Not including myself!
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/
atacarnet/src/com/evi/modules/atacarnet/action/
2. DebitNoteAction.class.getResource ("/")
Gets the absolute URI path of the current classpath.
such as: 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/
4. DebitNoteAction.class.getClassLoader (). GetResource ("") or Classloader.getsystemresource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/
5. Get Server relative path
System.getproperty ("User.dir")
For example: E:\apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin
I recommend using the Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") to get the URI representation of the absolute path of the current classpath
6. Get the absolute path in the project
General use Request.getrealpath ("/") or Request.getrealpath ("/config/")
But now do not advocate the use of Request.getrealpath ("/"), you can try the Servletcontext.getrealpath ("/") method to get the absolute path of the Web application's root directory
It is easy to get src files, because SRC is the default relative directory, such as you said to obtain the SRC under the COM directory of the Test.java file, you only need this is enough
File F = new file (Com/test.java);
But if I'm going to get files that are not in the SRC directory or Webroot directory, I'm going to take it from the SRC or webroot directory, like Doc.
my hard way is to achieve this:
String Path = This.getservletcontext (). Getrealpath ("/");
Properties P = new properties ();
P.load (New FileInputStream (path.substring (0, (Path.lastindexof ("\\WebRoot") + 1)) + "doc/db.properties"));
System.out.println (P.getproperty ("drivername"));
But I found a problem, that is, when I use IO stream access to the Web project Web-inf directory of files, will report an exception, tell me the relevant files can not find, do not know what the reason?

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.