Java gets the basic methods of various paths _java

Source: Internet
Author: User

This example for you to share the Java access to different paths for your reference, the specific content as follows

Package Com.ygh.blog.realpath;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;

Import java.util.Properties;

/** * Get the demo of the path below Java * * Import org.junit.Test; public class Realpathtest {/** * Gets the project path where the current class is located */@Test public void Fun1 () {File File = new file (This.getclass (). g
 Etresource ("/"). GetPath ());
 D:\project\taotaoshop\src\blog-mybatis1\target\test-classes System.out.println (file);
 /** * Gets the absolute path of the current class * * * @Test public void fun2 () {file = new file (This.getclass (). GetResource (""). GetPath ());
 D:\project\taotaoshop\src\blog-mybatis1\target\test-classes\com\ygh\blog\realpath System.out.println (file);  /** * Gets the project path where the current class is located, both of which can be * @throws IOException/@Test public void Fun3 () throws IOException {File File =
 New File ("");
 String path = File.getcanonicalpath ();
 D:\project\taotaoshop\src\blog-mybatis1 System.out.println (path); D:\project\taotaoshop\src\blog-mybatis1 System.out.println (System.getproperty ("User.dir")); /** * Gets the path to the file under current SRC */@Test public void Fun4 () {URL url = this.getclass (). getClassLoader (). GetResource ("jdbc.
 Properties ");
 System.out.println (URL); /** * Get other source packages under the file path */@Test public void Fun5 () {//Use this method to get the path URL url = This.getclass (). getClassLoader (). GE
 Tresource ("Test2.txt");
 File:/d:/project/taotaoshop/src/blog-mybatis1/target/classes/test.txt System.out.println (URL);
 @Test public void Fun6 () throws Exception {URL url = this.getclass (). getClassLoader (). GetResource ("Test2.txt");
 System.out.println (Url.getpath ());
 Properties Properties = new properties (); This way you can get the output stream of the file InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("
 Jdbc.properties ");
 Properties.load (InputStream);
 File File = new file (Url.getpath ());
 System.out.println (Properties.get ("Jdbc.driverclassname"));
 }

}

The file path corresponding to the code is assigned below

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.