網站首頁 編程語言 正文
關于class.getClassLoader().getResourceAsStream()和class.getResourceAsStream()的用法
作者:Jothan Zhong 更新時間: 2024-01-06 編程語言1.class.getResourceAsStream()
①.不加‘ / ’,直接在包的路徑下開始尋找,相當于在已經寫好的path前面加了一句 /包名/path
②加‘ / ’,就要從classpath的那一層開始找
比如,找到存放編譯后的class存放的位置:
//文件目錄
yx
jdbctest
com
A.class
B.class
1.properties```
2.properties
//調用1.properties
InputStream inputStream=PropertiesTest.class.getResourceAsStream("1.properties");
InputStream inputStream=PropertiesTest.class.getResourceAsStream("/yx/jdbctest/com/1.properties");
//調用2.properties
InputStream inputStream=PropertiesTest.class.getResourceAsStream("/2.properties");
123456789101112131415
2.class.getClassLoader().getResourceAsStream()
getClassLoader是直接從classpath開始查找文件的,因此使用這個就需要定位在classpath這個位置,也就是下面目錄中yx這一層。
//文件目錄
yx
jdbctest
com
A.class
B.class
1.properties```
2.properties
//調用1.properties
InputStream inputStream=PropertiesTest.class.getClassLoader().getResourceAsStream("yx/jdbctest/com/1.properties");
//調用2.properties
InputStream inputStream=PropertiesTest.class.getClassLoader().getResourceAsStream("2.properties");
12345678910111213
另外,如果你在整個工程目錄下建立一個resource文件,編譯之后,它會存到和yx同級的這個目錄下,也就是classpath這里,如果建立的是一個test resource 那就會在這一層的上面一層中,這個等以后寫了完整的程序出來,在做進一步的整理。
原文鏈接:https://blog.csdn.net/qq_43985303/article/details/135377550
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-05 HttpUtil發送外部請求包工具類
- 2022-02-02 ionic 禁止橫屏處理
- 2023-02-25 Golang嵌入資源文件實現步驟詳解_Golang
- 2022-11-14 C++資源管理操作方法詳解_C 語言
- 2023-07-04 HKDF秘鑰生成算法
- 2022-04-08 swift自定義表格控件(UITableView)_Swift
- 2022-07-19 Python?assert斷言聲明,遇到錯誤則立即返回問題_python
- 2022-12-13 iOS底層實例解析Swift閉包及OC閉包_IOS
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支