java读取配置文件的方法

1、基于ClassLoder读取配置文件

注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。

InputStream inputStream1 = PropertiesTest.class.getResourceAsStream("filepath");

2、基于 InputStream 读取配置文件

注意:该方式的优点在于可以读取任意路径下的配置文件

InputStream inputStream1 = new BufferedInputStream(new FileInputStream("filepath"));

3、举例:

(1)结构:

 (2)代码

public class PropertiesUtil {

    public static final PropertiesUtil INSTANCE = new PropertiesUtil();
    public static PropertiesUtil getInstance(){
        return INSTANCE;
    }

    /**
     * 1.使用getResourceAsStream读取配置文件
     */
    public void readPropertiesByGetSourceAsStream(){

        // 1.src下面com/demo/config包内的配置文件config1.properties
        InputStream inputStream1 = PropertiesTest.class.getClassLoader().getResourceAsStream("com/demo/config/config1.properties");

        // 2.读取和PropertiesTest位于同一个包内的配置文件config2.properties
        InputStream inputStream2 = PropertiesTest.class.getResourceAsStream("config2.properties");

        // 3.读取src根目录下的配置文件config3.properties
        InputStream inputStream3 = PropertiesTest.class.getClassLoader().getResourceAsStream("config3.properties");

        // 4.读取位于另外一个resource文件夹里面的配置文件config4.properties
        InputStream inputStream4 = PropertiesTest.class.getClassLoader().getResourceAsStream("config4.properties");

        Properties properties = new Properties();
        System.out.println("使用getResourceAsStream()配置文件...");

        try {
            System.out.println("-----读取inputStream1开始-----");
            properties.load(inputStream1);
            System.out.println("config1.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("url"));
            System.out.println("-----读取inputStream1结束-----");
            System.out.println("---------------------------------------------");

            System.out.println("-----读取inputStream2开始-----");
            properties.load(inputStream2);
            System.out.println("config2.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("url"));
            System.out.println("-----读取inputStream2结束-----");
            System.out.println("---------------------------------------------");

            System.out.println("-----读取inputStream3开始-----");
            properties.load(inputStream3);
            System.out.println("jdbc.properties:");
            // 这里的%s是java String占位符
            System.out.println(String.format("jdbc.driver=%s",properties.getProperty("jdbc.driver")));
            System.out.println(String.format("jdbc.url=%s",properties.getProperty("jdbc.url")));
            System.out.println(String.format("jdbc.username=%s",properties.getProperty("jdbc.username")));
            System.out.println(String.format("jdbc.password=%s",properties.getProperty("jdbc.password")));
            System.out.println("-----读取inputStream3结束-----");
            System.out.println("---------------------------------------------");

            System.out.println("-----读取inputStream4开始-----");
            properties.load(inputStream4);
            System.out.println("config.properties:");
            System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));
            System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));
            System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));
            System.out.println("-----读取inputStream4结束-----");
            System.out.println("---------------------------------------------");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(null != inputStream1){
                try {
                    inputStream1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != inputStream2){
                try {
                    inputStream2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != inputStream3){
                try {
                    inputStream3.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != inputStream4){
                try {
                    inputStream4.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    /**
     * 2.使用InputStream读取配置文件
     */
    public void readPropertiesByInputStream(){

        InputStream inputStream1 = null;
        InputStream inputStream2 = null;
        InputStream inputStream3 = null;
        InputStream inputStream4 = null;

        System.out.println("使用InputStream读取配置文件...");

        try {
            // 1.src下面com/demo/config包内的配置文件config1.properties
            inputStream1 = new BufferedInputStream(new FileInputStream("src/com/demo/config/config1.properties"));

            // 2.读取和PropertiesTest位于同一个包内的配置文件config2.properties
            inputStream2 = new BufferedInputStream(new FileInputStream("src/com/demo/day8/config2.properties"));

            // 3.读取src根目录下的配置文件config3.properties
            inputStream3 = new BufferedInputStream(new FileInputStream("src/config3.properties"));

            // 4.读取位于另外一个resource文件夹里面的配置文件config4.properties
            inputStream4 = new BufferedInputStream(new FileInputStream("resource/config4.properties"));

            Properties properties = new Properties();

            try {
                System.out.println("-----读取inputStream1开始-----");
                properties.load(inputStream1);
                System.out.println("config1.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("url"));
                System.out.println("-----读取inputStream1结束-----");
                System.out.println("---------------------------------------------");

                System.out.println("-----读取inputStream2开始-----");
                properties.load(inputStream2);
                System.out.println("config2.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("url"));
                System.out.println("-----读取inputStream2结束-----");
                System.out.println("---------------------------------------------");

                System.out.println("-----读取inputStream3开始-----");
                properties.load(inputStream3);
                System.out.println("jdbc.properties:");
                // 这里的%s是java String占位符
                System.out.println(String.format("jdbc.driver=%s", properties.getProperty("jdbc.driver")));
                System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));
                System.out.println(String.format("jdbc.username=%s", properties.getProperty("jdbc.username")));
                System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password")));
                System.out.println("-----读取inputStream3结束-----");
                System.out.println("---------------------------------------------");

                System.out.println("-----读取inputStream4开始-----");
                properties.load(inputStream4);
                System.out.println("config.properties:");
                System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));
                System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));
                System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));
                System.out.println("-----读取inputStream4结束-----");
                System.out.println("---------------------------------------------");

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                if (null != inputStream1) {
                    try {
                        inputStream1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (null != inputStream2) {
                    try {
                        inputStream2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (null != inputStream3) {
                    try {
                        inputStream3.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (null != inputStream4) {
                    try {
                        inputStream4.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

        }catch (FileNotFoundException e) {
                e.printStackTrace();
        }
    }

}


测试:
public class PropertiesTest {


    /**
     * 1.使用getSourceAsStream读取配置文件测试
     */
    @Test
    public void readPropertiesByGetSourceAsStreamTest(){
        PropertiesUtil.getInstance().readPropertiesByGetSourceAsStream();
    }

    /**
     * 2.使用InputStream读取配置文件测试
     */
    @Test
    public void readPropertiesByInputStreamTest(){
        PropertiesUtil.getInstance().readPropertiesByInputStream();
    }

}

测试结果:

 

 config4.properties不在测试类的路径下,不能通过ClassLoder的方式读取配置文件

 

 

 InputStream可以读取任何路径下的配置文件。

4)读取配置文件中的全部

以读取config3.properties为例;
public void readAllInfo()
{
    /*
     * 读取src根目录下面的配置文件
     * config3.properties
     */
    InputStream is3 = PropertiesTest.class.getClassLoader().getResourceAsStream("config3.properties");
    Properties p = new Properties();
    try
    {
        p.load(is3);
        //读取资源文件的所有的key值(列举)
        Enumeration en= p.propertyNames();
        System.out.println("读取is3开始...");
        while(en.hasMoreElements())
        {
            String key = (String) en.nextElement();
            System.out.println(key + "=" + p.getProperty(key));

        }
        System.out.println("读取is3结束...");
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.out.println("读取资源文件出错");
    }
    finally
    {
        if(null != is3)
        {
            try
            {
                is3.close();
                System.out.println("关闭is3...");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}

测试结果:

 

 

参考:https://www.cnblogs.com/sebastian-tyd/p/7895182.html

 

测试结果:
[Cèshì jiéguǒ:]
Test Results:
posted @ 2019-11-01 14:34  千堆雪  阅读(6015)  评论(0编辑  收藏  举报