Java gets the current path and reads the file
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 only aboutNew File (".")and theNew File ("..")There are two different paths.
for the Getcanonicalpath () function, the."represents the current folder, and 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 ();//Get isC:\TEST\ABC
Directory.getabsolutepath ();//Get isC:\test\abc
Direcotry.getpath (); Got ABC.
file directory = new file (".");
Directory.getcanonicalpath ();//Get isC:\test
Directory.getabsolutepath ();//Get isC:\test\.
Direcotry.getpath (); Got to be.
File directory = new file ("..");
Directory.getcanonicalpath ();//Get isC +
Directory.getabsolutepath ();//Get isC:\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 to current folder System.out.println (Directory.getabsolutepath ());//Get absolute path System.out.println (Directory.getpath ()); The Path System.out.println (System.getproperties ()) set when obtaining the new File ();/** * 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, files in this four source * path can be read */system.out.println (Thread.CurrentThread (). Getcontextclassloader () by writing the file name directly. 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 ();}}}
Java gets the current path and reads the file