Java class
System. getproperty ("user. dir ");
New file ("/")
Thread. currentthread (). getcontextclassloader (). getresource ("")
Filetest. Class. getclassloader (). getresource ("")
Classloader. getsystemresource ("")
Filetest. Class. getresource ("")
Filetest. Class. getresource ("/")
New file ("/"). getabsolutepath ()
JSP
Request. getrequesturi () -- absolute path corresponding to the root directory
Application. getrealpath (request. getrequesturi () -- absolute path of the object
Application. getrealpath ("/") -- absolute path of the current Web Application
New file (application. getrealpath (request. getrequesturi (). getparent () -- get the upper-level directory of the request file
Servletconfig. getservletcontext (). getrealpath ("/") -- absolute path of the current Web Application
Servlet
Request. getservletpath () -- absolute path corresponding to the root directory
Request. getsession (). getservletcontext (). getrealpath (request. getrequesturi () -- absolute path of the object
Server Path
1) WebLogic
New fileinputstream ("yourconfig/yourconf. properties ")
New fileinputstream ("./yourconfig/yourconf. properties ")
2) Tomcat
System. getproperty ("user. dir ")
1. system. getproperty ("user. dir ");
(User's current working directory.
Example: Write a Java filepathtest. Java; which is located under the basic package.
package basic;public class FilePathTest {public static void main(String[] args) {String sPath = System.getProperty("user.dir");System.out.println(sPath);}}
Use the javac command to compile the file into a. Class file (automatically generated in eclipse, which is located in the bin directory)
Test Example 1:
Set the basic and filepathtest. class files in the folder.
1) copy to the C root directory: Enter the DOS name and enter Java basic. filepathtest press ENTER ==> c :\
2) copy to the root directory of drive D: Enter the DOS name and enter Java basic. filepathtest press ENTER ==> D :\
Test Example 2:
Package this class into a jar file and execute "Java-jar jarname". The effect is the same as that in test example 1.
Test Example 3:
For a web project, this method varies depending on different Web servers.
For example, in Tomcat, the JSP method calls the tomcat installation root directory.
Therefore, this method is basically useless in web projects.