首页 > 技术文章 > java中读取properties文件

xbxblog 2018-11-01 19:17 原文

properties文件可以作为配置文件使用,文件中的数据是字典格式,如下:

name=abc
sex=male
address=12345

在项目中,properties文件可以放置在任意位置,不过一般都放在src/main/resources或者src/test/resources文件夹下。

在java代码中获取properties文件操作为:

  import java.util.Properties;

    public static String getProperties(String property) {
        String result = "";
        try {
            Properties prop = new Properties(); 
            FileInputStream in = new FileInputStream("src/main/resources/conf.properties");
            prop.load(in);
            result = prop.getProperty(property);
            in.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 

推荐阅读