Java gets the current path and reads the file

Source: Internet
Author: User

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

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.