关于读取Properties文件以及路径问题

时间:2021-12-14 00:33:46

1.本文并不全面,有待陆续的补充

2.目前先记录下,我项目中遇到过的。


java提供了对配置文件读取的工具类 java.util.Properties;

使用如下:

类名:ReadProperties

private static Properties prop = new Properties();
    static {
        try
        {
            InputStream fis = ReadProperties.class.getClassLoader()
            .getResourceAsStream("robotInterface.properties");
            prop.load(fis);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }


/**
 *获取robotInterface.properties文件中配置信息
 * @param paramName 属性名
 * @return 属性值String
 */

public static String read(String paramName)
    {
        return prop.getProperty(paramName);
    }



其中robotInterface.properties文件在src/main/resources路径下

项目使用的maven配置,所以我开始不知道要使用什么路径

最终使用ReadProperties.class.getClassLoader() .getResourceAsStream来作为输入流

prod.load  方法加载配置文件

prod.getProperty 通过key/value方式读取值。