最近有一个这样的需求,将原本配置文件 .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. vim复制到剪切板

    作者:whinc链接:https://www.zhihu.com/question/19863631/answer/89354508来源:知乎 转载文章 Vim 中的复制.删除的内容都会被存放到默认( ...

  2. git 清除所有历史记录

    有些时候,git 仓库累积了太多无用的历史更改,导致 clone 文件过大.如果确定历史更改没有意义,可以采用下述方法清空历史, 1) 先 clone 项目到本地目录 (以名为 mylearning ...

  3. 一键登录已成大势所趋,Android端操作指南来啦!

    根据极光(Aurora Mobile)发布的<2019年Q2移动互联网行业数据研究报告>,2019年第二季度,移动网民人均安装APP总量已达56款.面对如此繁多的APP,想在用户的手机中占 ...

  4. selenium设置Chrome浏览器不出现通知,设置代理IP

    from selenium import webdriver PROXY = "" chrome_options = webdriver.ChromeOptions() prefs ...

  5. QTP(1)

    一.概念 1.什么是软件测试? 使用人工或者自动手段来运行或者测试某个软件的过程,其目的在于检验程序是否满足需求规格说明书或者弄清实际结果与预期结果之间的差异. (1)软件(程序+文档+数据)测试 ( ...

  6. 第六章 组件 67 使用ref获取DOM元素和组件引用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  7. updatedepthtexture 和 screen space shadow 开关

    2018.0.3f 里面directional light开了shadow 就会有一张updatedepth 如果距离远 没有阴影就没有shadow pass 但是updatedepth没有关掉 管线 ...

  8. 记录weblogic12c通用版本在windowsXP上安装报异常的问题:java.lang.NoClassDefFoundError:

    先上图: 这个可能有经验的人一眼就看出问题了,报这个错的原因是:我把安装包放在了中文目录下,本来以为安装包放在中文目录下无关紧要,但是weblogic12c就是这么神奇,不能放在中文目录下. 解决方法 ...

  9. 小程序wx.showToast()方法实现文字换行

    小程序wx.showToast()方法实现文字换行 在文字中间加上  '\r\n' 真机中生效 wx.showToast({ title: `换行前内容\r\n换行后内容`, icon: 'none' ...

  10. 修改mysql5.7数据表字符集编码的命令

    查看表中字符集的命令 show variables like '%char%' 更改数据库中数据表的字符集靠谱命令,亲测可行,在workbench和phpmyadmin上都通过 alter table ...