java读取属性配置文件- Properties

本文介绍如何使用Java的Properties类来存储和读取配置文件信息。通过示例代码展示了如何加载和保存属性,以及如何更新属性值。此外还提供了一个模板样例,用于展示常见的配置项。

Properties的一个最有用的方面是可以利用store()方法和load()方法方便地对包含在属性(Properties)对象中的信息进行存储或从盘中装入信息。任何时候都可以将一个属性对象写入流或从中将其读出,这使得属性列表特别方便的实现简单的数据库。

     OutputStream outputStream = null;
        try
        {
            org.springframework.core.io.Resource resource = new ClassPathResource(
                    "/preschoolEdu.properties");
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            String templateUpdateDelay = properties.getProperty("template.update_delay");
            String messageCacheSeconds = properties.getProperty("message.cache_seconds");
            if (/*保存属性*/)
            {
                    outputStream = new FileOutputStream(resource.getFile());
                    properties.setProperty("template.update_delay", "0");
                    properties.setProperty("message.cache_seconds", "0");
                    properties.store(outputStream, "Sencloud PROPERTIES");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            //关闭I/O流
            IOUtils.closeQuietly(outputStream);
        }

#------------ Template ------------
template.encoding=UTF-8
template.update_delay=3600
template.number_format=0.######
template.boolean_format=true,false
template.datetime_format=yyyy-MM-dd
template.date_format=yyyy-MM-dd
template.time_format=HH:mm:ss
template.loader_path=/WEB-INF/template,classpath:/
template.suffix=.ftl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值