读写properties文件属性
来源:互联网 发布:江湖婚庆 3.0源码 编辑:程序博客网 时间:2024/06/05 21:54
说明:很多时候都是项目需求推动才会去找一些实现方式,然后草草的就实现了,想想不太对,决定将自己在工作中遇到的问题慢慢的进行总结,为自己也为别人方便。
需求大致如下,有个spriung boot项目的定时器,每天会执行一次,将扫描mongodb中的某一张表中的type为某一类型的数据,定时copy到mysql数据库中,为了效率问题,
需要将上一次扫描的最大id记录到一个单例的类中进行存储,但是,考虑到项目需要重启(不考虑序列化),就需要将该值持久化存储(暂时不考虑使用数据库等),
我有不想把一个id值存储到一个单独的文件当中,于是决定将该值存储到maven项目的properties文件中。若取到该值为初始化值(比如0)就取配置文件中的值。
在网上找了很多读取properties的资料,但是都好像不是很好用。于是:
public class PropertiesUtil implements Serializable{
private final static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
/**
* 序列化标识
*/
private static final long serialVersionUID = 1L;
private static Properties prop = new Properties();
private final static String file = "sysConfig.properties";
static{
FileInputStream in = null;
try {
in = new FileInputStream(file);
prop.load(in);
in.close();
} catch (FileNotFoundException e) {
logger.error("read sysConfig.properties : FileNotFoundException!",e);
} catch (IOException e) {
logger.error("read sysConfig.properties : IOException!",e);
}finally {
try {
if(in != null)in.close();
} catch (IOException e) {
logger.error("read sysConfig.properties ,close FileInputStream error!",e);
}
}
}
/**
* 获取属性的当前值
* @param key
* @return
*/
public static String getProperty(String key){
return prop.getProperty(key);
}
/**
* 将文件加载到内存中,在内存中修改key对应的value值,再将文件保存
* @param key
* @param value
*/
public static void setProper(String key,String value){
FileOutputStream fos = null;
try {
prop.setProperty(key, value);
fos = new FileOutputStream(file);
prop.store(fos, null);
fos.close();
}catch (FileNotFoundException e) {
logger.error("write sysConfig.properties : FileNotFoundException!",e);
} catch (IOException e) {
logger.error("write sysConfig.properties : IOException!",e);
}finally {
try {
if(fos != null)fos.close();
} catch (IOException e) {
logger.error("write sysConfig.properties error!",e);
}
}
}
}
需求大致如下,有个spriung boot项目的定时器,每天会执行一次,将扫描mongodb中的某一张表中的type为某一类型的数据,定时copy到mysql数据库中,为了效率问题,
需要将上一次扫描的最大id记录到一个单例的类中进行存储,但是,考虑到项目需要重启(不考虑序列化),就需要将该值持久化存储(暂时不考虑使用数据库等),
我有不想把一个id值存储到一个单独的文件当中,于是决定将该值存储到maven项目的properties文件中。若取到该值为初始化值(比如0)就取配置文件中的值。
在网上找了很多读取properties的资料,但是都好像不是很好用。于是:
public class PropertiesUtil implements Serializable{
private final static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
/**
* 序列化标识
*/
private static final long serialVersionUID = 1L;
private static Properties prop = new Properties();
private final static String file = "sysConfig.properties";
static{
FileInputStream in = null;
try {
in = new FileInputStream(file);
prop.load(in);
in.close();
} catch (FileNotFoundException e) {
logger.error("read sysConfig.properties : FileNotFoundException!",e);
} catch (IOException e) {
logger.error("read sysConfig.properties : IOException!",e);
}finally {
try {
if(in != null)in.close();
} catch (IOException e) {
logger.error("read sysConfig.properties ,close FileInputStream error!",e);
}
}
}
/**
* 获取属性的当前值
* @param key
* @return
*/
public static String getProperty(String key){
return prop.getProperty(key);
}
/**
* 将文件加载到内存中,在内存中修改key对应的value值,再将文件保存
* @param key
* @param value
*/
public static void setProper(String key,String value){
FileOutputStream fos = null;
try {
prop.setProperty(key, value);
fos = new FileOutputStream(file);
prop.store(fos, null);
fos.close();
}catch (FileNotFoundException e) {
logger.error("write sysConfig.properties : FileNotFoundException!",e);
} catch (IOException e) {
logger.error("write sysConfig.properties : IOException!",e);
}finally {
try {
if(fos != null)fos.close();
} catch (IOException e) {
logger.error("write sysConfig.properties error!",e);
}
}
}
}
阅读全文
0 0
- 读写properties文件属性
- 使用java读写properties文件属性
- java 读取properties文件属性
- springmvc注入properties文件属性
- Spring取得*.properties文件属性
- spring文件读取properties文件属性
- struts2标签引用Properties文件属性
- 读写properties
- properties文件属性值过长换行处理
- spring 使用注解获取properties 文件属性值
- Spring EmbeddedValueResolverAware 接口获取properties文件属性
- 通过Spring获取properties文件属性值
- spring项目properties文件属性配置到数据库
- Properties类读写.properties配置文件
- 一个读写ini文件属性的例子
- properties文件的读写
- .properties 文件读写
- Java读写Properties文件
- soapUI工具使用方法、简介、接口测试
- NFC和RFID的区别
- 设计模式(一):单例模式
- 使用bind标签创建变量
- 挑7
- 读写properties文件属性
- Python笔记(2)----5种标准数据类型
- 使用MultipartEntity上传图片文件
- 计算机的硬件组成
- 进程通信--总结
- Shell变量
- 王怡︱上帝的护理之工
- BZOJ1260[CQOI2007]涂色paint (区间DP)
- C#知识学习 遗忘的记忆 -第二天