Java gets the current path and reads the file

Source: Internet
Author: User

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

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.