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

感谢


作者:不敲代码的攻城狮
出处:https://www.cnblogs.com/leigq/
任何傻瓜都能写出计算机可以理解的代码。好的程序员能写出人能读懂的代码。

 
原文地址:https://www.cnblogs.com/leigq/p/13406533.html