Obtaining a relative path
Note: Relative paths (that is, without specifying who they are relative to at the time) can be obtained in the following ways (whether it is a general java project or a web project)
String relativelyPath=System.getProperty("user.dir");
In the above relative path, the files in the java project are relative to the root directory of the project
The file path in the web project varies depending on different web servers (tomcat is relative to the tomcat installation directorybin)
Obtaining the second class loading directory (that is, obtaining the loading directory of a certain class when running)
1.1) General method one (whether it is a general java project or a web project, first locate the first-level directory where you can see the package path)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(The path of the test.txt file is project namesrctest.txt; the first-level directory of the package where the class TestAction is located is under the src directory)
In the above formula, just replace TestAction and test.txt with the corresponding class name and file name.
1.2) General method two (this method is similar to the method in 1.1, except that this method must start with '/')
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(The path of the test.txt file is project namesrctest.txt, and the first-level directory of the package where class Test1 is located is under the src directory)
Obtaining the root directory of the three web projects (after release)
1 Starting from servlet
You can create a servlet and write the following statement in its init method
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (key)
The result looks like: D:ToolsTomcat-6.0webapps 02_ext (002_ext is the project name)
If s1.getRealPath("") is called, the output is D:ToolsTomcat-6.0webapps 02_ext (one less "")
2 Starting from httpServletRequest
String cp11111=request.getSession().getServletContext().getRealPath("/");
The result looks like: D:ToolsTomcat-6.0webapps 02_ext
Obtaining the fourth classpath (in Eclipse, obtaining the path to the src or classes directory)
Method 1 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
Output:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
Method 2 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse is a class in a certain package of src, the same below)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
Output: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
In addition, if you want to put the file in a certain package, you can obtain the file in the following way (first locate the last level directory of the package)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
Output: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse is the class in the jdom package in the src directory)