最近有一个这样的需求,将原本配置文件 .properties文件改到数据库当中,这样不用每次修改都重启服务器

java自带有处理 .properties文件的专有类 Properties,处理也很不错,但是没本法获取文件当中的注释,这就尴尬了,直接导入数据没有注释都不知道是个什么玩意,所以只能采用文件读取的方式获取。

具体实现如下:

 @BeforeClass
public static void getHiber(){
//1加载配置文件
Configuration config = new Configuration();
//默认加载src下hibernate.cfg.xml文件
config.configure("scy/hibernate.cfg.xml");
//2创建SessionFactory对象
SessionFactory factory = config.buildSessionFactory();
//SessionFactory sf=new Configuration().configure().buildSessionFactory();
session = factory.openSession();
}

代码采用hibernate+junit实现,设置好session之后就可以开始处理文件了

 @Test
public void getCommonFileLineTest(){
String path=SysParam.class.getResource("/").getPath().substring(1);
String currentLine = "";
Transaction tx = null ;
try{
File file = new File(path +"common.properties");
List<String> list = getCommonFileLine(file);
tx = session.beginTransaction();
for(int i=0;list!=null&&i<list.size();i++){
currentLine = list.get(i);
System.out.println(currentLine);
SysCommonSwitch switchs = new SysCommonSwitch();
if(currentLine.indexOf("#")>=0){
continue;
}else if(currentLine.indexOf("=")>0){
String[] arr = currentLine.split("=");
switchs.setCommonKey(arr[0]);
if(arr.length>=2 && StringUtils.isNotBlank(arr[1])){
switchs.setCommonValue(arr[1]);
}else{
switchs.setCommonValue("");
}
switchs.setDeletedFlag("0");
switchs.setSysId("CIS");
String proCurrentLine = list.get(i-1);
if(StringUtils.isNotBlank(proCurrentLine)){
String comment = proCurrentLine.replace("#", "");
switchs.setComment(comment);
}
session.save(switchs);
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
tx.commit();
session.close();
}
} private List<String> getCommonFileLine(File file){
ArrayList<String> arrayList = new ArrayList<String>();
BufferedReader bf = null;
try {
bf = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
String str;
// 按行读取字符串
while ((str = bf.readLine()) != null) {
arrayList.add(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("common行数"+arrayList.size());
return arrayList;
}

采用字符读取方式,按行读取,然后就是判断当前行是否是注释,需要注意的是,读取文件的时候一定要设置文件编码,不然读取出来的文字可能是乱码。

读取 .properties文件到数据库的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  3. Java读取properties文件连接数据库

    先说为什么要有这种东西,或者我们为什么要用这种方式来写,先看经常用的方法,我们经常写的 package util; import java.sql.Connection; import java.sq ...

  4. Spring 如何读取properties文件内容

    http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置 ...

  5. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  6. 用eclipse做项目中常遇到的问题-如何创建并读取properties文件

    在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...

  7. jsp读取properties文件

    jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...

  8. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

  9. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

随机推荐

  1. POJ1722 算法竞赛进阶指南 SUBSTRACT减操作

    原题连接 题目描述 给定一个整数数组\(a_1,a_2,-,a_n\). 定义数组第 i 位上的减操作:把\(a_i\)和\(a_{i+1}\)换成\(a_i - a_{i+1}\). 用con(a, ...

  2. 数据库——Oracle(2)

    1 插入语句(insert): 1) 往表中所有的列值都插入列值 SQL> desc person2; 名称 ID NAME 案例:往person2表中任意的插入3条数据 insert into ...

  3. mybatis整合Spring(一)

    DAO层:数据访问层 对远程存储系统做操作的一些程序统一放在DAO层 DAO层工作演变为:接口设计+SQL编写 DAO拼接等逻辑在Service层完成

  4. veeValidate实战

    说在前面 vee-validate 版本2.0.4的学习github地址我的项目地址第一次认真的在git上写一个demo教程,喜欢的可以star一下~^o^~ (^-^) (^o^) 后续会有一个完整 ...

  5. 快速取出选中checkbox的值

    原料:jquery 直接上代码 html代码 <html> <head> <title>$Title$</title> </head> &l ...

  6. Ion-select and ion-option list styling 自定义样式

    https://forum.ionicframework.com/t/ion-select-and-ion-option-list-styling/117028

  7. Chef 组件

    Chef是一家自动化公司.自2008年成立以来,我们一直在将开发人员和系统管理员与我们的同名产品Chef Infra联系在一起.多年来,我们所说的自动化已经扩展了.今天,Chef为基础设施和应用程序提 ...

  8. matplotlib(一):散点图

    import numpy as np import matplotlib.pyplot as plt #产生测试数据 # x,y为数组 N = 50 x = np.random.rand(N) y=n ...

  9. linux 查看内网IP和外网IP

    centos7 查看内网的ip,使用ifconfig 或在后面加上参数,都可以查看内网的ip,下面的10.105.33.17 即是内网的ip [root@VM_33_17_centos ~]#ifco ...

  10. Vue_(基础)商品管理-demo

    实现对商品的增加.删除.数量的修改功能 删除商品可选择直接删除当前商品.删除选中商品.删除所有商品 添加商品时会自动添加日期字段 商品的属性 goods : { id : '', name : '', ...