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.