Java中读取properties配置文件的八种方式

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
 
/**
 * @desc: Properties读取配置文件属性值的方式
 */
public class PropertiesTest {
 
    /**
     * 1. 方式一
     * 从当前的类加载器的getResourcesAsStream来获取
     * InputStream inputStream = this.getClass().getResourceAsStream(name)
     *
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("jdbc.url");
        System.out.println("property = " + property);
    }
 
    /**
     * 2. 方式二
     * 从当前的类加载器的getResourcesAsStream来获取
     * InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name)
     *
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("minio.endpoint");
        System.out.println("property = " + property);
    }
 
    /**
     * 3. 方式三
     * 使用Class类的getSystemResourceAsStream方法 和使用当前类的ClassLoader是一样的
     * InputStream inputStream = ClassLoader.getSystemResourceAsStream(name)
     *
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("minio.endpoint");
        System.out.println("property = " + property);
    }
 
    /**
     * 4. 方式四
     * Resource resource = new ClassPathResource(path)
     *
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        Resource resource = new ClassPathResource("config/application.properties");
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("minio.endpoint");
        System.out.println("property = " + property);
    }
 
    /**
     * 5. 方式五
     * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
     * BufferedInputStream继承自InputStream
     * InputStream inputStream = new BufferedInputStream(new FileInputStream(name)
     * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/application.properties"));
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("minio.endpoint");
        System.out.println("property = " + property);
    }
 
    /**
     * 6. 方式六
     * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
     * FileInputStream继承自InputStream
     * InputStream inputStream = new FileInputStream(name)
     * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
     * @throws IOException
     */
    @Test
    public void test6() throws IOException {
        InputStream inputStream = new FileInputStream("src/main/resources/config/application.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("minio.endpoint");
        System.out.println("property = " + property);
    }
 
    /**
     * 7. 方式七
     * 使用InputStream流来进行操作ResourceBundle,获取流的方式由以上几种。
     * ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
     * @throws IOException
     */
    @Test
    public void test7() throws IOException {
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
        ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
        Enumeration<String> keys = resourceBundle.getKeys();
        while (keys.hasMoreElements()) {
            String s = keys.nextElement();
            System.out.println(s + " = " + resourceBundle.getString(s));
        }
    }
 
    /**
     * 8. 方式八
     * ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,默认从根目录下读取,也可以读取resources目录下的文件
     * ResourceBundle rb = ResourceBundle.getBundle("b") //不需要指定文件名的后缀,只需要写文件名前缀即可
     */
    @Test
    public void test8(){
        //ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //读取resources目录下的jdbc.properties
        ResourceBundle rb2 = ResourceBundle.getBundle("config/application");//读取resources/config目录下的application.properties
        for(String key : rb2.keySet()){
            String value = rb2.getString(key);
            System.out.println(key + ":" + value);
        }
 
    }
 
 
 
    /**
     * 单独抽取的方法,用户检测能否正确操纵Properties
     *
     * @param inputStream
     * @throws IOException
     */
    private void printKeyValue(InputStream inputStream) throws IOException {
        Properties properties = new Properties();
        properties.load(inputStream);
        Set<Object> keys = properties.keySet();
        for (Object key : keys) {
            System.out.println(key + " = " + properties.get(key));
        }
    }
}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以通过Properties类来读取properties配置文件。具体步骤如下: 1. 创建Properties对象 Properties prop = new Properties(); 2. 加载配置文件 prop.load(new FileInputStream("config.properties")); 3. 读取配置文件的属性值 String value = prop.getProperty("key"); 其,key为配置文件的属性名,value为属性值。 需要注意的是,配置文件的路径需要根据实际情况进行修改,可以使用相对路径或绝对路径。另外,如果配置文件存在文字符,需要使用UTF-8编码格式进行读取。 ### 回答2: Java读取properties配置文件的方法很简单,可以借助JavaProperties类实现。 首先,需要引用java.util.Properties类,通过load()方法读取配置文件的数据,返回一个Properties对象。其配置文件可以是任何文件类型,但其结构必须按照.properties格式书写。在读取时,可以使用相对或绝对路径指定配置文件的位置。 读取配置文件的示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { FileInputStream in = new FileInputStream("config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 上述代码,首先创建了一个Properties对象,并通过调用load()方法读取配置文件config.properties的数据。然后,通过调用getProperty()方法分别获取了配置文件的url、user和password属性值,并将其输出到控制台。 另外,也可以使用Class.getResourceAsStream()或ClassLoader.getResourceAsStream()方法读取配置文件。示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { InputStream in = ReadProperties.class.getResourceAsStream("/config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 参考上述示例代码,读取properties配置文件非常简单,只需要使用JavaProperties类和相关方法即可。值得注意的是,在读取配置文件时,需要注意配置文件的位置及格式,以避免出现读取失败或读取错误的情况。 ### 回答3: properties 配置文件是一种常见的文本格式文件,其结构类似键值对。在 Java 读取 properties 配置文件非常方便,在 Java 的标准库已经提供了相关的 API。本文将介绍 Java 读取 properties 配置文件的方法。 Java 提供了 java.util.Properties 类用于操作 properties 文件。该类实现了 Map 接口,可以通过 key-value 的方式存储和操作属性,在 Java 读取 properties 文件通常需要进行两个步骤: 1.创建 Properties 对象,同时将 properties 文件的内容加载到 Properties 对象。 2.通过 Properties 对象获取流的配置值。 读取 Properties 文件,首先需要创建一个 Properties 对象,通常使用 Properties 类的 load 方法来加载 properties 文件,load 方法支持两种方法加载 properties 文件。其一是使用文件路径加载,其二是使用 InputStream 对象加载。 public void load(InputStream inStream) public void load(Reader reader) 对于第一种方式,看一下下面的代码: Properties properties = new Properties(); properties.load(new FileInputStream("/path/to/my.properties")); 对于第二种方式,将 InputStream 对象传递给 Properties 对象,调用 load 方法来读取内容: InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); Properties properties = new Properties(); properties.load(inputStream); 完成 Properties 文件的加载后,接下来就可以通过 getProperty() 方法获取到 key 对应的 value 内容了: String value = properties.getProperty("key"); 当然,Properties 对象还提供了很多其他的方法用于操作 properties 文件,例如,store() 方法用于存储 properties 文件, toString() 方法可用于将 Properties 对象转化为字符串等等。 总的来说,Java 读取 properties 配置文件非常简单,通过上述方法,可以轻松得到 properties 文件的配置项,完成自己关于 properties 文件的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值