读取 .properties文件到数据库

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

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

具体实现如下:

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

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

 1 @Test
 2     public void getCommonFileLineTest(){
 3         String path=SysParam.class.getResource("/").getPath().substring(1);
 4         String currentLine = "";
 5         Transaction tx = null ;
 6         try{
 7             File file = new File(path +"common.properties");
 8             List<String> list = getCommonFileLine(file);
 9             tx = session.beginTransaction();
10             for(int i=0;list!=null&&i<list.size();i++){
11                 currentLine = list.get(i);
12                 System.out.println(currentLine);
13                 SysCommonSwitch switchs = new SysCommonSwitch();
14                 if(currentLine.indexOf("#")>=0){
15                     continue;
16                 }else if(currentLine.indexOf("=")>0){
17                     String[] arr = currentLine.split("=");
18                     switchs.setCommonKey(arr[0]);
19                     if(arr.length>=2 && StringUtils.isNotBlank(arr[1])){
20                         switchs.setCommonValue(arr[1]);
21                     }else{
22                         switchs.setCommonValue("");
23                     }
24                     switchs.setDeletedFlag("0");
25                     switchs.setSysId("CIS");
26                     String proCurrentLine = list.get(i-1);
27                     if(StringUtils.isNotBlank(proCurrentLine)){
28                         String comment = proCurrentLine.replace("#", "");
29                         switchs.setComment(comment);
30                     }
31                     session.save(switchs);
32                 }
33             }
34         }catch(Exception e){
35             e.printStackTrace();
36         }finally{
37             tx.commit();
38             session.close();
39         }
40     }
41     
42     private List<String> getCommonFileLine(File file){
43         ArrayList<String> arrayList = new ArrayList<String>();
45         BufferedReader bf = null;
46         try {
49             bf = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
50             String str;
51             // 按行读取字符串
52             while ((str = bf.readLine()) != null) {
53                 arrayList.add(str);
54             }
55         } catch (FileNotFoundException e) {
56             // TODO Auto-generated catch block
57             e.printStackTrace();
58         } catch (IOException e) {
59             // TODO Auto-generated catch block
60             e.printStackTrace();
61         }
62         System.out.println("common行数"+arrayList.size());
63         return arrayList;
64     }

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

 

posted @ 2019-03-11 18:13  平心先生  阅读(532)  评论(0编辑  收藏  举报