Java to obtain the path of a file or folder.

Source: Internet
Author: User

Java to obtain the path of a file or folder.

Obtain the project path of the current class. If "/" File f = new File (this. getClass (). getResource (""). getPath (); System. out. println (f); Result: C: \ Documents ents % 20and % 20Settings \ Administrator \ workspace \ projectName \ bin \ com \ test to obtain the absolute path of the current class; Type 2: file directory = new File (""); // The parameter is null String courseFile = directory. getCanonicalPath (); System. out. println (courseFile); Result: C: \ Documents ents and Settings \ Administrator \ workspace \ projectName: Obtain the project path of the current class; Third: URL xmlpath = this. getClass (). getClassLoader (). getResource ("selected.txt"); System. out. println (xmlpath); Result: file:/C:/Documents % 20and % 20 Settings/Administrator/workspace/projectName/bin/selected.txtgets the selected.txt file path in the srcdirectory of the current project. Type 4: System. out. println (System. getProperty ("user. dir "); Result: C: \ Documents and Settings \ Administrator \ workspace \ projectName: System. out. println (System. getProperty ("java. class. path "); Result: C: \ Documents and Settings \ Administrator \ workspace \ projectName \ bin get the current project path

 

Related Article

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.