java中读取properties文件

properties文件一般都是框架中的配置文件,在框架中一般都是自动加载。如果我们自己手动编写代码应该如何加载?

properties文件本质还是一个文本文件,只要是文件,那么我们就可以通过java语言提供的I/O流API把文件读入到内存中。利用java.util.Properties工具类对流进行解析。

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadPropertiesDemo {
    
    public static void main(String[] args) throws IOException {
        InputStream inputStream = ReadPropertiesDemo.class.getResourceAsStream("/application.properties");
        Properties properties=new Properties();
        properties.load(inputStream);
        System.out.println(properties.get("name"));;
    }
}
原文地址:https://www.cnblogs.com/cplinux/p/14079387.html