この記事は、参照のためにJavaでさまざまなパスを取得する方法を共有しています。特定のコンテンツは次のとおりです
パッケージcom.ygh.blog.realpath; Import java.io.file; import java.io.io.ioexception; Import java.io.inputStream; import java.net.url; Import java.util.properties;配置*/ @test public void fun1(){file file = new file(this.getClass()。getResource( "/")。getPath()); // d:/project/taotaoshop/src/blog-mybatis1/target/test-classes system.out.println(file); } / ***現在のクラスへの絶対パスを取得* / @test public void fun2(){file file = new file(this.getClass()。getResource( "")。getPath()); // d:/project/taotaoshop/src/blog-mybatis1/ターゲット/テストクラス/com/ygh/blog/realpath system.out.println(file); } /***現在のクラスがあるプロジェクトパスを取得します。両方の方法を使用できます * * @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")); } / ***現在のsrc* / @test public void fun4(){url url = this.getClass()。getClassOour()。getResource( "jdbc.properties"); System.out.println(url); } / ***他のソースコードパッケージの下のファイルパスを取得* / @test public void fun5(){//このメソッドを使用してpath url url = this.getClass()。getClassOour()。getResource( "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()。getClassolder()。getResource( "test2.txt"); System.out.println(url.getPath());プロパティプロパティ= new Properties(); //このメソッドを使用して、ファイルに対応する出力ストリームを取得しますinputstream inputstream = this.getClass()。getClassLoader()。getResourceasStream( "jdbc.properties"); properties.load(inputstream); file file = new file(url.getPath()); system.out.println(properties.get( "jdbc.driverclassname")); }}以下は、コードに対応するファイルパスです
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。