Java獲取路徑的6種方式代碼示例
public class Demo1 { public static void main(String[] args) { /* 1.使用 System 屬性 */ // 獲取用戶的主目錄 String userHome = System.getProperty("user.home"); System.out.println("User Home: " + userHome); // 獲取Java的安裝目錄 String javaHome = System.getProperty("java.home"); System.out.println("Java Home: " + javaHome); /* * 2.使用 ClassLoader 獲取資源路徑 */ // 獲取類路徑下的資源文件路徑 ClassLoader classLoader = Demo1.class.getClassLoader(); URL resourceUrl = classLoader.getResource("config.properties"); String resourcePath = resourceUrl != null ? resourceUrl.getPath() : null; System.out.println("Resource Path: " + resourcePath); /* 3.使用 File 類 */ // 創(chuàng)建一個(gè)File對(duì)象 File file = new File("example.txt"); // 獲取絕對(duì)路徑 String absolutePath = file.getAbsolutePath(); System.out.println("Absolute Path: " + absolutePath); // 獲取相對(duì)路徑(相對(duì)于當(dāng)前工作目錄) String canonicalPath; try { canonicalPath = file.getCanonicalPath(); System.out.println("Canonical Path: " + canonicalPath); } catch (IOException e) { e.printStackTrace(); } // 獲取父目錄路徑 String parentPath = file.getParent(); System.out.println("Parent Path: " + parentPath); /* 4.使用 Paths 類(Java 7及以上) */ // 獲取當(dāng)前工作目錄 Path currentDir = Paths.get(".").toAbsolutePath(); System.out.println("Current Directory: " + currentDir); // 拼接路徑 Path filePath = Paths.get(currentDir.toString(), "example.txt"); System.out.println("File Path: " + filePath); /* 5.使用 URI */ File file2 = new File("example.txt"); URI uri = file2.toURI(); String uriPath = uri.getPath(); System.out.println("URI Path: " + uriPath); /* 6. 獲取當(dāng)前執(zhí)行文件的路徑(Java應(yīng)用) */ String path = Demo1.class.getProtectionDomain().getCodeSource().getLocation().getPath(); System.out.println("Executable Path: " + path); } }
總結(jié)
到此這篇關(guān)于Java獲取路徑的6種方式的文章就介紹到這了,更多相關(guān)Java獲取路徑方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java通過(guò)JNI調(diào)用C++動(dòng)態(tài)庫(kù)的完整流程詳解
JNI(Java Native Interface),是實(shí)現(xiàn)Java/Kotlin與C/C++語(yǔ)言之間交互的橋梁,本文主要為大家介紹了Java通過(guò)JNI調(diào)用C++動(dòng)態(tài)庫(kù)的完整流程,需要的可以參考下2025-04-04redis發(fā)布訂閱Java代碼實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了redis發(fā)布訂閱Java代碼實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09intellij idea隱藏.iml和.idea等自動(dòng)生成文件的問(wèn)題
這篇文章主要介紹了intellij idea隱藏.iml和.idea等自動(dòng)生成文件的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09java觀察者模式的三種實(shí)現(xiàn)方式代碼實(shí)例
這篇文章主要介紹了java觀察者模式的三種實(shí)現(xiàn)方式代碼實(shí)例,觀察者模式(又被稱為發(fā)布-訂閱(Publish/Subscribe)模式,屬于行為型模式的一種,它定義了一種一對(duì)多的依賴關(guān)系,讓多個(gè)觀察者對(duì)象同時(shí)監(jiān)聽(tīng)某一個(gè)主題對(duì)象,需要的朋友可以參考下2023-10-10使用Spring Boot輕松實(shí)現(xiàn)流式AI輸出的步驟
本文介紹了如何使用Spring Boot和WebFlux實(shí)現(xiàn)流式AI輸出,通過(guò)非阻塞I/O、反應(yīng)式編程和函數(shù)式路由等技術(shù),優(yōu)化了AI應(yīng)用的響應(yīng)速度,提升了用戶體驗(yàn),感興趣的朋友一起看看吧2025-02-02Java實(shí)現(xiàn)根據(jù)sql動(dòng)態(tài)查詢并下載數(shù)據(jù)到excel
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)根據(jù)sql動(dòng)態(tài)查詢并下載數(shù)據(jù)到excel的功能,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-04-04Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹
這篇文章主要介紹了Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹的相關(guān)資料,需要的朋友可以參考下2017-03-03使用maven項(xiàng)目pom.xml文件配置打包功能和靜態(tài)資源文件自帶版本號(hào)功能
在Maven項(xiàng)目中,通過(guò)pom.xml文件配置打包功能,可以控制構(gòu)建過(guò)程,生成可部署的包,同時(shí),為了緩存控制與版本更新,可以在打包時(shí)給靜態(tài)資源文件如JS、CSS添加版本號(hào),這通常通過(guò)插件如maven-resources-plugin實(shí)現(xiàn)2024-09-09