1. Use the System.getproperty () function to get the current path:
System.out.println (System.getproperty ("User.dir"));//user.dir Specifies the current path
2. Use the function provided by file to get the current path:
File directory = new file (""); Set as current folder
System.out.println (Directory.getcanonicalpath ());//Get the standard path
System.out.println (Directory.getabsolutepath ());//Get absolute path
File.getcanonicalpath () and File.getabsolutepath () are approximately different from the two paths of the new file (".") and the new file ("...").
For the Getcanonicalpath () function, ". "Represents the current folder, while the". "Represents the top level folder in the current folder
For the GetAbsolutePath () function, regardless of the ". ”、“ .. ", return the current path plus the path you set at New File ()
As for the GetPath () function, you get only the path you set at the new File ()
For example, the current path is C:\test:
File directory = new file ("abc");
Directory.getcanonicalpath (); Got the C:\TEST\ABC.
Directory.getabsolutepath (); Got to be
C:\test\abc
Direcotry.getpath (); Got ABC.
File directory = new file (".");
Directory.getcanonicalpath (); Got the C:\test.
Directory.getabsolutepath (); Got to be
C:\test\.
Directory.getcanonicalpath (); Get the C + +
Directory.getabsolutepath (); Got to be
C:\test\.
Direcotry.getpath (); What you get is that.
The source code is as follows: public class Readpropertiesutil {
/**
@param
Args
*/
public static void Main (string[] args) {
File directory = new file (""); Set as current folder
System.out.println (Directory.getabsolutepath ()); Get absolute path
System.out.println (Directory.getpath ()); The path set when obtaining the new File ()
System.out.println (System.getproperties ());
/**
* Class.getclassloader (). getResourceAsStream ($path), where $path is the SRC source path by default, and the MAVEN project is typically configured with multiple source paths
* For example: The source path for MAVEN projects is: Src/main/java,src/main/resources,src/test/java,src/test/resources, in this four source
* Files under the path can be read directly by writing the file name
*/
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Com/wpy/json/data.properties")) ;
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("db.properties"));
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Src/test/java/file1.properties" ));
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("file2.properties"));
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("file3.properties"));
ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();
System.out.println (ClassLoader);
if (ClassLoader = = null) {
Properties Iframeproperties = new properties ();
ClassLoader = Iframeproperties.getclass (). getClassLoader ();
}
System.out.println (ClassLoader);
try {
System.out.println (Directory.getcanonicalpath ()); Get the standard path
} catch (IOException e) {
E.printstacktrace ();
}
}
- This article is from: Hobby Technology Network
- This article link: http://www.ahlinux.com/java/19784.html
Java gets the current path and reads the file