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

609

被折叠的 条评论
为什么被折叠?



