读取.properties配置文件的几种方式:

这里我将配置文件放在src目录下了,下面放的都是实验过的代码:

第一种(通过输入流):

        //1、获取到流文件
        FileInputStream is = new FileInputStream("src/jdbc.properties");
//2、加载流文件
        Properties properties = new Properties();
        properties.load(is);
        //3、获取到配置文件信息
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String name = properties.getProperty("name");
        String passwd = properties.getProperty("passwd");

第二种(通过类加载器):

        //1、获取到流文件
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
     //2、加载流文件
        Properties properties = new Properties();
        properties.load(is);
        //3、获取到配置文件信息
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String name = properties.getProperty("name");
        String passwd = properties.getProperty("passwd");

第三种(通过资源绑定器):

        //1、获取到流文件
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        //2、获取到配置文件信息
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String name = bundle.getString("name");
        String passwd = bundle.getString("passwd");
原文地址:https://www.cnblogs.com/zhangzhixi/p/14193364.html