java讀取資源檔案的N種方法

2021-05-27 10:54:03 字數 1520 閱讀 4267

如何讀取資源檔案:

(一)properties props = new properties();

props.load(new fileinputstream("db.properties"));

(二)blog.properties檔案如下

dbdriver=oracle.jdbc.driver.oracledriver

dburl=jdbc:oracle:thin:@127.0.0.1:1521:ora92

dbuser=blog

dbpwd=blog

--------------

public class config

public string dbdriver;

public string dbuser;

public string dbpwd;

public string dburl;

private void initdbconfig()

catch (exception ex) }}

----------------

public class dao

public connection getconnection()

catch (exception ex)

return conn;}}

(三)properties props=new properties();

props.load(bugfactory. class.getresourceasstream("xx.properties"));

string name = props.getpropery("***x");

此時xx.properties應該與該類放在同乙個目錄.

(四)resourcebundle res = resourcebundle.getbundle("yy.properties");

string name = res.getstring("yyyy");

yy.properties應放在/web-inf/classes目錄

(五)如果你這個bean打包的話,就把這個檔案放在包內。

我一般是這樣寫的

properties prop = new properties();

try另:

props.load(new fileinputstream("db.properties")); 是讀取當前目錄的db.properties檔案

getclass.getresourceasstream("db.properties"); 是讀取當前類所在位置一起的db.properties檔案

getclass.getresourceasstream("/db.properties"); 是讀取classpath的根的db.properties檔案,注意classpath如果是多個路徑或者jar檔案的,只要在任意乙個路徑目錄下或者 jar檔案裡的根下都可以,如果存在於多個路徑下的話,按照classpath中的先後順序,使用先找到的,其餘忽略

** :

讀取資源檔案

資源檔案在存放彈出資訊很有用,以下是讀資源檔案的簡單 dim rm as resourcemanager new resourcemanager readresx.message system.reflection.assembly.getexecutingassembly dim ci as cu...

android讀取資源檔案的方法

方法一 把目標檔案放入resources檔案中,以通過讀取r的資源檔案來獲取,具體方式如下 1 在res下新建raw檔案,將帶讀取檔案新增到raw檔案目錄下。2 新增如下 如果要使用檔名獲取檔案資料 首先獲取資源id然後再通過id獲取輸入流 string filename filename stri...

java讀取jar中的資源檔案

需求 已經打包成jar,需要讀取其中的資源檔案。思路 直接讀取其對應的jar檔案路徑以bufferedreader方式讀取 實現 獲取到reader public class dicreader catch unsupportedencodingexception e return null 基於r...