Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604987
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-10 11:54:12


点击(此处)折叠或打开


  1. package org.fh.util;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.net.URLDecoder;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;

  10. /**
  11.  * 说明:ini文件读写操作工具类
  12.  * 作者:FH Admin
  13.  * from:fhadmin.cn
  14.  */
  15. public class IniFileUtil {

  16.     /**
  17.      * 从ini配置文件中读取变量的值
  18.      * @param file 配置文件的路径
  19.      * @param section 要获取的变量所在段名称
  20.      * @param variable 要获取的变量名称
  21.      * @param defaultValue 变量名称不存在时的默认值
  22.      * @return 变量的值
  23.      * @throws IOException 抛出文件操作可能出现的io异常
  24.      */
  25.     public static String readCfgValue(String file, String section, String variable, String defaultValue)
  26.             throws IOException {
  27.         String strLine, value = "";
  28.         BufferedReader bufferedReader = new BufferedReader(new FileReader(URLDecoder.decode(file, "UTF-8"))); //解决中文路径乱码
  29.         boolean isInSection = false;
  30.         try {
  31.             while ((strLine = bufferedReader.readLine()) != null) {
  32.                 strLine = strLine.trim();
  33.                 strLine = strLine.split("[;]")[0];
  34.                 Pattern p;
  35.                 Matcher m;
  36.                 p = Pattern.compile("\\[\\w+]");// Pattern.compile("file://[//s*.*//s*//]");
  37.                 m = p.matcher((strLine));
  38.                 if (m.matches()) {
  39.                     p = Pattern.compile("\\[" + section + "\\]");// Pattern.compile("file://[//s*" + section +
  40.                                                                     // "file://s*//]");
  41.                     m = p.matcher(strLine);
  42.                     if (m.matches()) {
  43.                         isInSection = true;
  44.                     } else {
  45.                         isInSection = false;
  46.                     }
  47.                 }
  48.                 if (isInSection == true) {
  49.                     strLine = strLine.trim();
  50.                     String[] strArray = strLine.split("=");
  51.                     if (strArray.length == 1) {
  52.                         value = strArray[0].trim();
  53.                         if (value.equalsIgnoreCase(variable)) {
  54.                             value = "";
  55.                             return value;
  56.                         }
  57.                     } else if (strArray.length == 2) {
  58.                         value = strArray[0].trim();
  59.                         if (value.equalsIgnoreCase(variable)) {
  60.                             value = strArray[1].trim();
  61.                             return value;
  62.                         }
  63.                     } else if (strArray.length > 2) {
  64.                         value = strArray[0].trim();
  65.                         if (value.equalsIgnoreCase(variable)) {
  66.                             value = strLine.substring(strLine.indexOf("=") + 1).trim();
  67.                             return value;
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         } finally {
  73.             bufferedReader.close();
  74.         }
  75.         return defaultValue;
  76.     }

  77.     /**
  78.      * 修改ini配置文件中变量的值
  79.      * @param file 配置文件的路径
  80.      * @param section 要修改的变量所在段名称
  81.      * @param variable 要修改的变量名称
  82.      * @param value 变量的新值
  83.      * @throws IOException 抛出文件操作可能出现的io异常
  84.      */
  85.     public static boolean writeCfgValue(String file, String section, String variable, String value) throws IOException {
  86.         String fileContent, allLine, strLine, newLine;
  87.         String getValue = null;
  88.         BufferedReader bufferedReader = new BufferedReader(new FileReader(URLDecoder.decode(file, "UTF-8"))); //解决中文路径乱码
  89.         boolean isInSection = false;
  90.         boolean canAdd = true;
  91.         fileContent = "";
  92.         try {

  93.             while ((allLine = bufferedReader.readLine()) != null) {
  94.                 allLine = allLine.trim();
  95.                 strLine = allLine.split(";")[0];
  96.                 Pattern p;
  97.                 Matcher m;
  98.                 p = Pattern.compile("\\[\\w+]");
  99.                 m = p.matcher((strLine));
  100.                 if (m.matches()) {
  101.                     p = Pattern.compile("\\[" + section + "\\]");
  102.                     m = p.matcher(strLine);
  103.                     if (m.matches()) {
  104.                         isInSection = true;
  105.                     } else {
  106.                         isInSection = false;
  107.                     }
  108.                 }
  109.                 if (isInSection == true) {
  110.                     strLine = strLine.trim();
  111.                     String[] strArray = strLine.split("=");
  112.                     getValue = strArray[0].trim();
  113.                     if (getValue.equalsIgnoreCase(variable)) {
  114.                         newLine = getValue + "=" + value;
  115.                         fileContent += newLine;
  116.                         while ((allLine = bufferedReader.readLine()) != null) {
  117.                             fileContent += "\r\n" + allLine;
  118.                         }
  119.                         bufferedReader.close();
  120.                         canAdd = false;
  121.                         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
  122.                         bufferedWriter.write(fileContent);
  123.                         bufferedWriter.flush();
  124.                         bufferedWriter.close();

  125.                         return true;
  126.                     }

  127.                 }
  128.                 fileContent += allLine + "\r\n";
  129.             }
  130.             if (canAdd) {
  131.                 String str = variable + "=" + value;
  132.                 fileContent += str + "\r\n";
  133.                 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
  134.                 bufferedWriter.write(fileContent);
  135.                 bufferedWriter.flush();
  136.                 bufferedWriter.close();
  137.             }
  138.         } catch (IOException ex) {
  139.             throw ex;
  140.         } finally {
  141.             bufferedReader.close();
  142.         }
  143.         return false;
  144.     }

  145.     public static void main(String[] args) {
  146.         try {
  147.             /*;文件事例
  148.             [Client]
  149.             ;客户端版本号
  150.             version=0001
  151.             ;设备号
  152.             devNum=6405*/
  153.             String value = IniFileUtil.readCfgValue("L:/a.ini", "Client", "devNum", "1");
  154.             System.out.println(value);
  155.         } catch (IOException e) {
  156.             e.printStackTrace();
  157.         }
  158.     }
  159.     
  160. }


阅读(507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~