Multiple methods for obtaining the current file path in java

Source: Internet
Author: User

Multiple methods for obtaining the current file path in java


Public String getClassPath (){
String path = "";
Try {
Path = new File (getClass (). getClassLoader (). getResource (""). toURI (). getPath ();
} Catch (URISyntaxException ex ){}
Return path;
}

Obtain the directory path when the program is running. That is, the path where the program double-click or the BAT file is located

System. getProperties ("user. dir ");

 


First:
File f = new File (this. getClass (). getResource ("/"). getPath ());
System. out. println (f );
Result:
C: Documents % 20and % 20 SettingsAdministratorworkspaceprojectNamebin
Obtain the Project path of the current class;
If "/" is not added,
File f = new File (this. getClass (). getResource (""). getPath ());
System. out. println (f );
Result:
C: Documents % 20and % 20 SettingsAdministratorworkspaceprojectNamebincomtest
Obtain the absolute path of the current class;

Second:
File directory = new File (""); // The parameter is null.
String courseFile = directory. getCanonicalPath ();
System. out. println (courseFile );
Result:
C: Documents and SettingsAdministratorworkspaceprojectName
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 Ents % 20and %

20 Settings/Administrator/workspace/projectName/bin/selected.txt
Obtain the path of selected.txt in the srcdirectory of the current project

Fourth:
System. out. println (System. getProperty ("user. dir "));
Result:
C: Documents and SettingsAdministratorworkspaceprojectName
Get Current Project path

Category 5:
System. out. println (System. getProperty ("java. class. path "));
Result:
C: Documents and SettingsAdministratorworkspaceprojectNamebin
Get Current Project path

Absolute path and relative path:
The absolute path is the real path of the file or directory on your home page on the hard disk (URL and physical path), for example: C: xyz

Est.txt represents the absolute path of the test.txt file. Http://www.111cn.net also represents a URL absolute

Path. Relative path: the path relative to a base directory. Contains the relative path of the Web (relative directory in HTML)

For example, in Servlet, "/" indicates the Directory of the Web application. The relative representation of the physical path. Example: "./" generation

The current directory of the table. "../" indicates the parent directory. This similar representation is also a relative path. In addition, for URI,

For more information, see RFC standards. RFC 2396: Uniform Resource Identifiers

(URI): Generic Syntax

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.