Java and file paths

Source: Internet
Author: User

First: Get the absolute path of the current class;
If you do not add "/"

File F = new file (This.getclass (). GetResource (""). GetPath ());
System.out.println (f);

Result: C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test

Second: Get the engineering path of the current class;

File directory = new file ("");//parameter is empty
String coursefile = Directory.getcanonicalpath ();
System.out.println (Coursefile);

Result: C:\Documents and Settings\administrator\workspace\projectname

Third: Get the path of the Selected.txt file under the current Engineering src directory

URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("Selected.txt");
System.out.println (Xmlpath);

Result: File:/c:/documents%20and%20settings/administrator/workspace/projectname/bin/selected.txt

Fourth: Get Current engineering path

System.out.println (System.getproperty ("User.dir"));

Result: C:\Documents and Settings\administrator\workspace\projectname

Fifth: Get current engineering path

System.out.println (System.getproperty ("Java.class.path"));

Result: C:\Documents and Settings\administrator\workspace\projectname\bin

View MORE:
Java Project get file path summary

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.