读取配置文件资料

该代码示例展示了如何使用Java的Properties类从输入流中加载配置文件config.properties,并获取其中的user、company1、company2和company3等属性。程序首先通过getClass().getClassLoader().getResourceAsStream()方法获取资源流,然后加载属性并打印相关信息,最后关闭输入流。
摘要由CSDN通过智能技术生成

 

关于配置文件

核心思想,

        1.io流

        2.Prooerties类

    public void test2() {
        InputStream input = null;

        Properties prop = new Properties();
        String propFileName = "config.properties";

        input = getClass().getClassLoader().getResourceAsStream(propFileName);

        if (input != null) {
            try {
                prop.load(input);
                int available = input.available();
                System.out.println("Available: " + available);
                String user = prop.getProperty("user");
                String company1 = prop.getProperty("company1");
                String company2 = prop.getProperty("company2");
                String company3 = prop.getProperty("company3");

                Date time = new Date(System.currentTimeMillis());
                String result = "Company List = " + company1 + ", " + company2 + ", " + company3;
                System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
                input.close();
            } catch (Exception e) {
                System.out.println("Exception: " + e);
            } finally {
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值