Java 讀取 Properties 文件

讀取方式

一、Java 原生

利用java.util自帶的Properties類讀取

Properties類的load方法提供了兩種讀取文件的方式:

一、基於ClassLoder讀取配置文件

該方式只能讀取類路徑下的配置文件,有侷限但是如果配置文件在類路徑下比較方便。

public static void main(String[] args) throws IOException {
    Properties properties = new Properties();
    // 使用 ClassLoader 加載 properties 配置文件生成對應的輸入流
    InputStream in = PortraitUtils.class.getClassLoader().getResourceAsStream("student.properties");
    // 使用 properties 對象加載輸入流
    properties.load(in);
    // 獲取 key 對應的 value 值
    System.out.println(String.format("name = %s", properties.getProperty("name")));
    System.out.println(String.format("age = %s", properties.getProperty("age")));
}

二、基於 InputStream 讀取配置文件

該方式的優點在於可以讀取任意路徑下的配置文件,不過一般項目中不會這麼用,項目中一般用第一種方式。

public static void main(String[] args) throws IOException {
    Properties properties = new Properties();
    // 使用InPutStream流讀取properties文件
    BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\IdeaProjects\\springboot-building\\src\\main\\resources\\student.properties"));
    properties.load(bufferedReader);
    // 獲取key對應的value值
    System.out.println(String.format("name = %s", properties.getProperty("name")));
    System.out.println(String.format("age = %s", properties.getProperty("age")));
}

三、通過 java.util.ResourceBundle 類來讀取,這種方式比使用 Properties 要方便一些

  • 通過 ResourceBundle.getBundle() 靜態方法來獲取(ResourceBundle是一個抽象類),這種方式來獲取properties屬性文件不需要加.properties後綴名,只需要文件名即可
public static void main(String[] args) throws IOException {
    ResourceBundle resource = ResourceBundle.getBundle("student");
    System.out.println(String.format("name = %s", resource.getString("name")));
    System.out.println(String.format("age = %s", resource.getString("age")));
}
  • 從 InputStream 中讀取,獲取 InputStream 的方法和上面一樣,不再贅述
public static void main(String[] args) throws IOException {
    InputStream inStream = PortraitUtils.class.getClassLoader().getResourceAsStream("student.properties");
    ResourceBundle resource = new PropertyResourceBundle(inStream);
    System.out.println(String.format("name = %s", resource.getString("name")));
    System.out.println(String.format("age = %s", resource.getString("age")));
}

PropertyResourceBundle 中還是調用的 properties.load() 方法,源碼如下:

public PropertyResourceBundle (InputStream stream) throws IOException {
    Properties properties = new Properties();
    properties.load(stream);
    lookup = new HashMap(properties);
}

二、Spring

主要使用了spring-core-4.1.4.RELEASE-sources.jar 這個jar包裏的 PropertiesLoaderUtils.loadProperties 方法。

其中 Properties props ,java.util.Properties是對properties這類配置文件的映射,支持key-value類型和xml類型兩種。

properties類實現了Map接口,所以很明顯,他是用map來存儲key-value數據,所以也註定存入數據是無序的,這個點需要注意。

針對key-value這種配置文件,是用load方法就能直接映射成map,非常簡單好用。

public static void main(String[] args) throws IOException {
    /*方式一*/
    Resource resource = new ClassPathResource("student.properties");
    Properties props = PropertiesLoaderUtils.loadProperties(resource);
    System.out.println(String.format("name = %s", props.getProperty("name")));
    System.out.println(String.format("age = %s", props.getProperty("age")));

    // 可以直接強轉成 Map
    Map<String,String> param = new HashMap<String,String>((Map) props);
    System.out.println(String.format("name = %s", param.get("name")));
    System.out.println(String.format("age = %s", param.get("age")));

    /*方式二*/
    Properties properties = PropertiesLoaderUtils.loadAllProperties("student.properties");
    System.out.println(String.format("name = %s", properties.getProperty("name")));
    System.out.println(String.format("age = %s", properties.getProperty("age")));
}

上面的輸出結果爲:

20190815202638.png

感謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章