Multiple ways to get 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;
}
Directory path when the program is run. The path where the program double-clicks or the bat file is located
System.getproperties ("User.dir");
The first type:
File F = new file (This.getclass (). GetResource ("/"). GetPath ());
System.out.println (f);
Results:
C:documents%20and%20settingsadministratorworkspaceprojectnamebin
Gets the project path where the current class is located;
If you do not add "/"
File F = new file (This.getclass (). GetResource (""). GetPath ());
System.out.println (f);
Results:
C:documents%20and%20settingsadministratorworkspaceprojectnamebincomtest
Gets the absolute path of the current class;
The second type:
File directory = new file ("");//parameter is empty
String coursefile = Directory.getcanonicalpath ();
System.out.println (Coursefile);
Results:
C:documents and Settingsadministratorworkspaceprojectname
Gets the project path where the current class is located;
The third type:
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("Selected.txt");
System.out.println (Xmlpath);
Results:
file:/c:/documents%20and%
20settings/administrator/workspace/projectname/bin/selected.txt
Gets the path to the Selected.txt file in the current project SRC directory
The fourth type:
System.out.println (System.getproperty ("User.dir"));
Results:
C:documents and Settingsadministratorworkspaceprojectname
Get current project path
The fifth type:
System.out.println (System.getproperty ("Java.class.path"));
Results:
C:documents and Settingsadministratorworkspaceprojectnamebin
Get current project path
Absolute path and relative path:
The absolute path is the real path on the hard disk of the file or directory on your home page, (URL and physical path) for example: C:XYZ
Est.txt represents the absolute path to 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 to the web (relative to the directory in HTML)
, for example: In a servlet, "/" represents a directory of Web apps. and the relative representation of the physical path. For example: "./" Generation
Table current directory, ".. /"represents the parent directory. This similar representation also belongs to the relative path. In addition about the URI,
Url,urn and other content, please refer to the RFC related documentation standards. RFC 2396:uniform Resource Identifiers
(URI): Generic Syntax
[Above excerpt from: http://www.111cn.net/jsp/Java/39141.htm]
Multiple ways to get the current file path in Java