You often need to get a path to a file in your project:
Here are some ways to get the path.
1. The path obtained in this way is the path where the current class resides: Java code userdaotest. class. GetResource ("Userdaotest.class")
UserDAOTest.class.getResource ("Userdaotest.class")
The results are as follows:
File:/e:/java/workspace/testproject/webcontent/web-inf/classes/com/java/business/dao/userdaotest.class
2. This way gets the path of the current classpath: Java code thread.currentthread (). Getcontextclassloader (). GetResource ("")
Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")
The results are as follows:
file:/e:/java/workspace/testproject/webcontent/web-inf/classes/
3. This way gets the path to the system resource file. If empty, gets the path where the classpath is located. Java Code Classloader.getsystemresource ("")
Classloader.getsystemresource ("")
The results are as follows:
file:/e:/java/workspace/testproject/webcontent/web-inf/classes/
4. This way gets the path to the Userdaotest class file. Java code userdaotest. class. GetResource ("")
UserDAOTest.class.getResource ("")
The results are as follows:
file:/e:/java/workspace/testproject/webcontent/web-inf/classes/com/business/dao/
5. This way, get the disk where the project is located. Java code new File ("/"). GetAbsolutePath ()
New File ("/"). GetAbsolutePath ()
The results are as follows:
E:\
6. In this way, get the absolute path of the project name. Java code system.getproperty ("User.dir")
System.getproperty ("User.dir")
The results are as follows:
E:\java\workspace\testProject