1. 程式人生 > >Java讀取Properties文件的六種方法

Java讀取Properties文件的六種方法

java讀取properties文件的方法

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);


本文出自 “10916470” 博客,轉載請與作者聯系!

Java讀取Properties文件的六種方法