Java Fetch Path __java

Source: Internet
Author: User

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

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.