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