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提供軟件國際化的捷徑,這個類的作用一般是用來讀取資源屬性文件(properties),然后根據.properties文件的名稱信息(本地化信息),匹配當前系統的國別語言信息(也可以程序指定),然后獲取相應的properties文件的內容。
注意:,這個properties文件的名字是有規范的:一般的命名規范是: 自定義名_語言代碼_國別代碼.properties,
如果是默認的,直接寫為:自定義名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
當在中文操作系統下,如果myres_zh_CN.properties、myres.properties兩個文件都存在,則優先會使用myres_zh_CN.properties,當myres_zh_CN.properties不存在時候,會使用默認的myres.properties。
例:定義三個資源文件,放到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"));
}
}結果:
好
好
good
3。使用java.util.PropertyResourceBundle類的構造函數
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
PropertyResourceBundle是ResourceBundle的具體子類,是通過對屬性文件的靜態字符串管理來語言環境資源。與其他資源包類型不同,不能為 PropertyResourceBundle 創建子類。相反,要提供含有資源數據的屬性文件。ResourceBundle.getBundle 將自動查找合適的屬性文件并創建引用該文件的 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對象,getResourceAsStream返回的是java.io包inputStream
例2:

在上面的目錄中,有一個src目錄,那么,我們在Test類中應該如何分別獲得
其中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());獲取不同路徑下文件傳入的參數不相同。當傳入的參數是沒有”/”的時候,獲取的是當前類所在包下的對應文件。而當參數帶有”/”,則是從ClassPath根目錄下獲取文件。該方法的本質其實只是通過傳入path構造一個絕對路徑,最終還是由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()靜態方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
補充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
創新互聯www.cdcxhl.cn,專業提供香港、美國云服務器,動態BGP最優骨干路由自動選擇,持續穩定高效的網絡助力業務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節活動現已開啟,新人活動云服務器買多久送多久。
文章名稱:Java讀取Properties文件的六種方法-創新互聯
網頁鏈接:http://www.yijiale78.com/article6/docsig.html
成都網站建設公司_創新互聯,為您提供建站公司、網站改版、Google、微信公眾號、搜索引擎優化、電子商務
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯