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; /** * 獲取java下面的路徑的演示 */ import org.junit.Test; public class RealPathTest { /** * 獲取當(dāng)前類所在的工程路徑 */ @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); } /** * 獲取當(dāng)前類的絕對路徑 */ @Test public void fun2() { File file = new File(this.getClass().getResource("").getPath()); // D:\project\taotaoshop\src\blog-mybatis1\target\test-classes\com\ygh\blog\realpath System.out.println(file); } /** * 獲取當(dāng)前類所在的工程路徑,兩種方法皆可 * * @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")); } /** * 獲取當(dāng)前src下面的文件的路徑 */ @Test public void fun4() { URL url = this.getClass().getClassLoader().getResource("jdbc.properties"); System.out.println(url); } /** * 獲取其他源碼包下面的文件路徑 */ @Test public void fun5() { // 使用這種方法可以獲取路徑 URL url = this.getClass().getClassLoader().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().getClassLoader().getResource("test2.txt"); System.out.println(url.getPath()); Properties properties = new Properties(); // 使用這種方式可以獲取文件對應(yīng)的輸出流 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(inputStream); File file = new File(url.getPath()); System.out.println(properties.get("jdbc.driverClassName")); } }
下面賦上代碼對應(yīng)的文件路徑
本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章
Grass Wonder Build Guide |烏瑪媽媽漂亮的德比
1 個月前
By Jack chen
<??>:在森林里99夜 - 所有徽章以及如何解鎖
4 周前
By DDD
烏瑪?shù)姆劢z漂亮的德比橫幅日程(2025年7月)
1 個月前
By Jack chen
Rimworld Odyssey溫度指南和Gravtech
3 周前
By Jack chen
Windows安全是空白或不顯示選項
1 個月前
By 下次還敢

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)