1. 程式人生 > >java 中文件的讀取File、以及相對路徑的問題

java 中文件的讀取File、以及相對路徑的問題

forname property getclass 輸入 代碼 eas 結果 NPU more

內容轉自: https://blog.csdn.net/fk1778770286/article/details/53900636

1 Properties properties = new Properties();
2 InputStream is = DBUtils.class.getResourceAsStream("jdbc.properties");

以下寫法,從class根目錄查找文件

1 Properties properties= new Properties();
2 InputStream inStream= DBUtil.class.getClassLoader().getResourceAsStream("configjdbc.properties");
3 properties.load(inStream);

以下寫法從當前類所在package下查找文件

1 Properties properties= new Properties();
2 InputStream inStream= DBUtil.class.getResourceAsStream("configjdbc.properties");
3 properties.load(inStream);

====================以下來自轉載=============

由java中的數據庫配置文件引入代碼,想到文件加載的方式和路徑問題

  

一、對於java項目中文件的讀取

1、使用System 或是 系統的Properties對象

		①直接是使用  String relativelyPath=System.getProperty("user.dir");
②使用Properties對象 我們先來遍歷一下系統的屬性: Properties properties = System.getProperties();
Enumeration pnames = properties.propertyNames();
while (pnames.hasMoreElements()) {
String pname = (String) pnames.nextElement();
System.out.print(pname + "--------------");
System.out.println(properties.getProperty(pname));
}

給大家截個圖:

技術分享圖片

這是系統的屬性,由此其實還是繞到使用 user.dir 屬性來取得當前項目的真是路徑

通過 String relativelyPath = properties.getProperty("user.dir"); 取得

我自己的電腦上面的項目 Log4jProj 的真是路徑是 :

user.dir--------------D:\Develop\workspace\ws_self\10_ws_eclipse_j2ee_mars\Log4jProj

其實方式①和方式②一個意思,殊途同歸

2、第二種方式:使用當前類的類加載器進行獲取 ClassLoader

首先來回顧一下,如何獲取Class字節碼實例,三種方式:(比如我的類叫Demo)

① Demo.class

② Class.forName("類的全稱")

③ 利用Demo的實例對象,調用對象的getClass()方法獲取該對象的Class實例

回顧了如何獲取Class字節碼實例之後,然後再來回顧一下,如何獲取ClassLoader對象

① Demo.class.getClassLoader()

② Class.forName("類的全稱").getClassLoader()

③ 假設demo為Demo的實例化對象 demo.getClass().getClassLoader()

④ 通過Thread對象的getContextClassLoader() 方法來獲取

Thread.currentThread().getContextClassLoader()

進入正題:

有了ClassLoader對象之後,我們這麽時候通過ClassLoader對象來獲取java項目中的文件

首先讓大家看下我當前的項目目錄結構

技術分享圖片

以及實際文件的目錄結構

技術分享圖片

技術分享圖片

需求就是,此時Test需要讀取 log4j.properties 文件的路徑

用到ClassLoader的兩個方法,一個是靜態的一個非靜態的

技術分享圖片

技術分享圖片

技術分享圖片

輸出結果:

技術分享圖片

記住哦,這裏的getSystemResource方法獲取的是URL對象,需要調用getPath()方法獲取路徑

1、當只是獲取 log4j.properties 文件輸入流的時候可以通過以下兩種方式

① 依然是使用 ClassLoader, 其中有兩個方法,兩者一個是靜態一個非靜態

技術分享圖片

技術分享圖片

ClassLoader.getSystemResourceAsStream("config/log4j.properties");

Thread.currentThread().getContextClassLoader().getResourceAsStream("config/log4j.properties");

② 先通過File文件包裝之後,然後新建一個輸入流

File file01 = new File("config/log4j.properties");
System.out.println(file01.getAbsolutePath());

File file02 = new File(properties.getProperty("user.dir") + "/bin/config/log4j.properties");
System.out.println(file02.getAbsolutePath());

//ClassLoader.getSystemResource獲取的是URL對象
File file03 = new File(ClassLoader.getSystemResource("config/log4j.properties").getPath());
System.out.println(file03.getAbsolutePath());

其中創建file03 的方式不建議采納,因為getSystemResource方法如果沒獲取到文件,則得到的

URL對象為null,此時再調用getPath()就會報錯

如果有了文件對象就可以直接創建流了,此處不作贅述

java 中文件的讀取File、以及相對路徑的問題