In depth analysis of the Java file path details

  • 2020-04-01 01:57:46
  • OfStack

There are two types of paths used in Java: absolute paths and relative paths. After all, Java essentially USES only absolute paths to find resources. All relative path methods for finding resources are simply convenience methods. But the API at the bottom helped us build the absolute path to find the resource!
When developing Web applications, it is often necessary to obtain the physical path of the current WebRoot in the server.
If the Servlet, Action, Controller, or Filter, the Listener, the relevant classes such as interceptors, we just need to get the ServletContext, then through the ServletContext. GetRealPath ("/") to obtain the current application on the server's physical address.
If a ServletContext is not available in a class, there are two ways to do this:
1. Call XXX. Class. GetClassLoader (). Method to get to the ClassPath and then process to get the WebRoot directory only if the class is under WebRoot/ web-inf /classes and if the class is packaged into a jar file, the method fails. Here's the way to do it.
2. The idea of spring framework: in web-inf /web.xml, create a param of webAppRootKey, specify a value (default is webapp.root) as the key value, and then execute String webAppRootKey = getServletContext(). GetRealPath ("/") through Listener, Filter, or Servlet. The webapp.root corresponding to the webAppRootKey is written to the System Properties as the Key and Value respectively. The physical path of WebRoot is then obtained in the program through system.getproperty ("webapp.root").
We can expand on the second line of thinking. But for applications deployed on a single server, look again if that's not what you need.
Here are some ways to get the classpath and the absolute path of the current class. You can use some of these methods to get the absolute path to the resource you need:
1. DebitNoteAction. Class. GetResource (" ")
You get the URI directory for the current 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 ("/")
You get the absolute URI path of the current classpath.
Such as: file: / D: / eclipse/springTest/WebRoot/WEB - INF/classes /
3. Thread. CurrentThread (). GetContextClassLoader (). The getResource (" ")
You also get the absolute URI path of the current ClassPath
Such as: file: / D: / eclipse/springTest/WebRoot/WEB - INF/classes /
4. DebitNoteAction. Class. GetClassLoader () getResource (" ") or this. GetSystemResource (" ")
You also get the absolute URI path of the current ClassPath.
Such as: file: / D: / eclipse/springTest/WebRoot/WEB - INF/classes /
5. Get the relative path of the server
System. GetProperty (" user. Dir ")
For example: E: \ apache tomcat - 5.5.16 \ apache tomcat -- 5.5.16 \ bin
I recommend using 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
Request-getrealpath ("/") or request-getrealpath ("/config/")
But now I do not advocate using request. GetRealPath ("/"), you can try ServletContext getRealPath ("/") method get absolute path to the root directory of the Web application
It is very easy to get the SRC files because SRC is the default relative directory. For example, if you say you want to get the test.java files in the com directory under SRC, that's all you need
File f = new File(com/test.java);
But what if I want to get a file that's not in the SRC directory or the WebRoot directory, but from the same directory as SRC or WebRoot, say doc
My hard method works like this:
String path = this. GetServletContext (.) getRealPath ("/");
Properties p = new Properties();
New FileInputStream(new File(path. Substring (0, path. LastIndexOf ("\ WebRoot") + "doc/db.properties"));
System. The out. Println (p.g etProperty (" driverName "));
However, I found a problem, that is, when I used the IO stream to access the files in the web-inf directory of the web project, it would report an exception, telling me that the relevant files could not be found, I do not know why?

Related articles: