Java讀取Properties文件有以下六種方法:

1。使用java.util.Properties類的load()方法
String fileName="E:/system.properties"; InputStream in = new BufferedInputStream(new FileInputStream(fileName)); Properties p = new Properties(); p.load(in); System.out.println(p);
2。使用java.util.ResourceBundle類的getBundle()方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
ResourceBundle提供軟件國(guó)際化的捷徑,這個(gè)類的作用一般是用來讀取資源屬性文件(properties),然后根據(jù).properties文件的名稱信息(本地化信息),匹配當(dāng)前系統(tǒng)的國(guó)別語言信息(也可以程序指定),然后獲取相應(yīng)的properties文件的內(nèi)容。
注意:,這個(gè)properties文件的名字是有規(guī)范的:一般的命名規(guī)范是: 自定義名_語言代碼_國(guó)別代碼.properties,
如果是默認(rèn)的,直接寫為:自定義名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
當(dāng)在中文操作系統(tǒng)下,如果myres_zh_CN.properties、myres.properties兩個(gè)文件都存在,則優(yōu)先會(huì)使用myres_zh_CN.properties,當(dāng)myres_zh_CN.properties不存在時(shí)候,會(huì)使用默認(rèn)的myres.properties。
例:定義三個(gè)資源文件,放到src的根目錄下面,:
myres.properties
aaa=good
bbb=thanks
myres_en_US.properties
aaa=good
bbb=thanks
myres_zh_CN.properties
aaa=\u597d
bbb=\u591a\u8c22
import java.util.Locale;
import java.util.ResourceBundle;
public class TestResourceBundle {
public static void main(String[] args) {
Locale locale1 = new Locale("zh", "CN");
ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);
System.out.println(resb1.getString("aaa"));
ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());
System.out.println(resb2.getString("aaa"));
Locale locale3 = new Locale("en", "US");
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
}
}結(jié)果:
好
好
good
3。使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
PropertyResourceBundle是ResourceBundle的具體子類,是通過對(duì)屬性文件的靜態(tài)字符串管理來語言環(huán)境資源。與其他資源包類型不同,不能為 PropertyResourceBundle 創(chuàng)建子類。相反,要提供含有資源數(shù)據(jù)的屬性文件。ResourceBundle.getBundle 將自動(dòng)查找合適的屬性文件并創(chuàng)建引用該文件的 PropertyResourceBundle
4。使用java.lang包的class變量的getResourceAsStream()方法
InputStream in = 類名.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
例:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
public class FileGet {
private final static String SYSFILE="/system.properties";
public static void main(String[] args) throws Exception {
File file = new File((FileGet.class.getResource(SYSFILE)).getFile());
InputStream in = new BufferedInputStream(new FileInputStream(file));
Properties p = new Properties();
p.load(in);
System.out.println(p);
InputStream in2 = FileGet.class.getResourceAsStream(SYSFILE);
Properties p2 = new Properties();
p2.load(in2);
System.out.println(p2);
}
}getResource返回的是java.net包的URL對(duì)象,getResourceAsStream返回的是java.io包inputStream
例2:

在上面的目錄中,有一個(gè)src目錄,那么,我們?cè)赥est類中應(yīng)該如何分別獲得
其中file.txt可以通過這兩種方式來獲取:
方法一:
File file1 = new File(Test.class.getResource("file.txt").getFile());方法二:
File file2 = new File(Test.class.getResource("/com/file.txt").getFile());file2.txt獲取方法:
File file3 = new File(Test.class.getResource("/file2.txt").getFile());獲取不同路徑下文件傳入的參數(shù)不相同。當(dāng)傳入的參數(shù)是沒有”/”的時(shí)候,獲取的是當(dāng)前類所在包下的對(duì)應(yīng)文件。而當(dāng)參數(shù)帶有”/”,則是從ClassPath根目錄下獲取文件。該方法的本質(zhì)其實(shí)只是通過傳入path構(gòu)造一個(gè)絕對(duì)路徑,最終還是由ClassLoader獲取資源。
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = 類名.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);
6。使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
補(bǔ)充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
文章名稱:Java讀取Properties文件的六種方法-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.chinadenli.net/article6/docsig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站改版、Google、微信公眾號(hào)、搜索引擎優(yōu)化、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容