java 中檔案的讀取File、以及相對路徑的問題

來源:互聯網
上載者:User

標籤:eclips   電腦   相對路徑   getpath   article   extc   如何擷取   dir   回顧   

內容轉自: 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、以及相對路徑的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.