java读取和修改ini配置文件

 iLife's 博客http://blog.csdn.net/fei1502816 




  1. package mytools;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.util.regex.Matcher;  
  9. import java.util.regex.Pattern;  
  10.   
  11. /** 
  12.  * 这是个配置文档操作类,用来读取和配置ini配置文档 
  13.  *  
  14.  * @author 由月 
  15.  * @version 2004-08-18 
  16.  * @修改 2008-05-22 
  17.  */  
  18. public final class ConfigurationFile {  
  19.     /** 
  20.      * 从ini配置文档中读取变量的值 
  21.      *  
  22.      * @param file 
  23.      *            配置文档的路径 
  24.      * @param section 
  25.      *            要获取的变量所在段名称 
  26.      * @param variable 
  27.      *            要获取的变量名称 
  28.      * @param defaultValue 
  29.      *            变量名称不存在时的默认值 
  30.      * @return 变量的值 
  31.      * @throws IOException 
  32.      *             抛出文档操作可能出现的io异常 
  33.      */  
  34.     public static String getProfileString(String file, String section,  
  35.             String variable, String defaultValue) throws IOException {  
  36.         String strLine, value = "";  
  37.         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
  38.         boolean isInSection = false;  
  39.         try {  
  40.             while ((strLine = bufferedReader.readLine()) != null) {  
  41.                 strLine = strLine.trim();  
  42.                 // strLine = strLine.split("[;]")[0];  
  43.                 Pattern p;  
  44.                 Matcher m;  
  45.                 p = Pattern.compile("[url=file://[.*//]\\[.*\\]");  
  46.                 m = p.matcher((strLine));  
  47.                 if (m.matches()) {  
  48.                     p = Pattern.compile("[url=file://[(.*)//]\\[(.*)\\]");  
  49.                     m = p.matcher(strLine);  
  50.                     if (m.matches()) {  
  51.                         isInSection = true;  
  52.                     } else {  
  53.                         isInSection = false;  
  54.                     }  
  55.                 }  
  56.                 if (isInSection == true) {  
  57.                     strLine = strLine.trim();  
  58.                     String[] strArray = strLine.split("=");  
  59.                     if (strArray.length == 1) {  
  60.                         value = strArray[0].trim();  
  61.                         if (value.equalsIgnoreCase(variable)) {  
  62.                             value = "";  
  63.                             return value;  
  64.                         }  
  65.                     } else if (strArray.length == 2) {  
  66.                         value = strArray[0].trim();  
  67.                         if (value.equalsIgnoreCase(variable)) {  
  68.                             value = strArray[1].trim();  
  69.                             return value;  
  70.                         }  
  71.                     } else if (strArray.length > 2) {  
  72.                         value = strArray[0].trim();  
  73.                         if (value.equalsIgnoreCase(variable)) {  
  74.                             value = strLine.substring(strLine.indexOf("=") + 1)  
  75.                                     .trim();  
  76.                             return value;  
  77.                         }  
  78.                     }  
  79.                 }  
  80.             }  
  81.         } finally {  
  82.             bufferedReader.close();  
  83.         }  
  84.         return defaultValue;  
  85.     }  
  86.   
  87.     /** 
  88.      * 修改ini配置文档中变量的值 
  89.      *  
  90.      * @param file 
  91.      *            配置文档的路径 
  92.      * @param section 
  93.      *            要修改的变量所在段名称 
  94.      * @param variable 
  95.      *            要修改的变量名称 
  96.      * @param value 
  97.      *            变量的新值 
  98.      * @throws IOException 
  99.      *             抛出文档操作可能出现的io异常 
  100.      */  
  101.     public static boolean setProfileString(String file, String section,  
  102.             String variable, String value) throws IOException {  
  103.         String fileContent, allLine, strLine, newLine, remarkStr;  
  104.         String getValue;  
  105.         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
  106.         boolean isInSection = false;  
  107.         fileContent = "";  
  108.         try {  
  109.   
  110.             while ((allLine = bufferedReader.readLine()) != null) {  
  111.                 allLine = allLine.trim();  
  112.                 System.out.println("allLine == " + allLine);  
  113.                 // if (allLine.split("[;]").length > 1)  
  114.                 // remarkStr = ";" + allLine.split(";")[1];  
  115.                 // else  
  116.                 // remarkStr = "";  
  117.                 // strLine = allLine.split(";")[0];  
  118.                 strLine = allLine;  
  119.                 Pattern p;  
  120.                 Matcher m;  
  121.                 p = Pattern.compile("[url=file://[.*//]\\[.*\\]");  
  122.                 m = p.matcher((strLine));  
  123.                 if (m.matches()) {  
  124.                     p = Pattern.compile("[url=file://[(.*)//]\\[(.*)\\]");  
  125.                     m = p.matcher(strLine);  
  126.                     if (m.matches()) {  
  127.                         isInSection = true;  
  128.                     } else {  
  129.                         isInSection = false;  
  130.                     }  
  131.                 }  
  132.                 if (isInSection == true) {  
  133.                     strLine = strLine.trim();  
  134.                     String[] strArray = strLine.split("=");  
  135.                     getValue = strArray[0].trim();  
  136.                     if (getValue.equalsIgnoreCase(variable)) {  
  137.                         // newLine = getValue + " = " + value + " " + remarkStr;  
  138.                         newLine = getValue + " = " + value + " ";  
  139.                         fileContent += newLine + "\r\n";  
  140.                         while ((allLine = bufferedReader.readLine()) != null) {  
  141.                             fileContent += allLine + "\r\n";  
  142.                         }  
  143.                         bufferedReader.close();  
  144.                         BufferedWriter bufferedWriter = new BufferedWriter(  
  145.                                 new FileWriter(file, false));  
  146.                         bufferedWriter.write(fileContent);  
  147.                         bufferedWriter.flush();  
  148.                         bufferedWriter.close();  
  149.   
  150.                         return true;  
  151.                     }  
  152.                 }  
  153.                 fileContent += allLine + "\r\n";  
  154.             }  
  155.         } catch (IOException ex) {  
  156.             throw ex;  
  157.         } finally {  
  158.             bufferedReader.close();  
  159.         }  
  160.         return false;  
  161.     }  
  162.   
  163.     /** 
  164.      * 程式测试 
  165.      */  
  166.     public static void main(String[] args) {  
  167.         // String value = Config.getProfileString("sysconfig.ini", "Option",  
  168.         // "OracleDB", "default");  
  169.         // System.out.println(value);  
  170.         try {  
  171.             System.out.println("值已经改变!... "  
  172.                     + ConfigurationFile.setProfileString(  
  173.                             "F:/update/gamewww.ini""TestSect1""1001",  
  174.                             "111111"));  
  175.             System.out.println("值读取成功!... "  
  176.                     + ConfigurationFile.getProfileString(  
  177.                             "F:/update/gamewww.ini""TestSect1""1001"""));  
  178.         } catch (IOException e) {  
  179.             System.out.println("错误 ......" + e.toString());  
  180.         }  
  181.     }  
  182. }  


配置文件格式:

[TestSect1]
1001 = 111111
1002 = 222222
1003 = 333333  
1004 = 444444
1005 = 555555  
1006 = 666666
  1. package mytools;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.util.regex.Matcher;  
  9. import java.util.regex.Pattern;  
  10.   
  11. /** 
  12.  * 这是个配置文档操作类,用来读取和配置ini配置文档 
  13.  *  
  14.  * @author 由月 
  15.  * @version 2004-08-18 
  16.  * @修改 2008-05-22 
  17.  */  
  18. public final class ConfigurationFile {  
  19.     /** 
  20.      * 从ini配置文档中读取变量的值 
  21.      *  
  22.      * @param file 
  23.      *            配置文档的路径 
  24.      * @param section 
  25.      *            要获取的变量所在段名称 
  26.      * @param variable 
  27.      *            要获取的变量名称 
  28.      * @param defaultValue 
  29.      *            变量名称不存在时的默认值 
  30.      * @return 变量的值 
  31.      * @throws IOException 
  32.      *             抛出文档操作可能出现的io异常 
  33.      */  
  34.     public static String getProfileString(String file, String section,  
  35.             String variable, String defaultValue) throws IOException {  
  36.         String strLine, value = "";  
  37.         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
  38.         boolean isInSection = false;  
  39.         try {  
  40.             while ((strLine = bufferedReader.readLine()) != null) {  
  41.                 strLine = strLine.trim();  
  42.                 // strLine = strLine.split("[;]")[0];  
  43.                 Pattern p;  
  44.                 Matcher m;  
  45.                 p = Pattern.compile("[url=file://[.*//]\\[.*\\]");  
  46.                 m = p.matcher((strLine));  
  47.                 if (m.matches()) {  
  48.                     p = Pattern.compile("[url=file://[(.*)//]\\[(.*)\\]");  
  49.                     m = p.matcher(strLine);  
  50.                     if (m.matches()) {  
  51.                         isInSection = true;  
  52.                     } else {  
  53.                         isInSection = false;  
  54.                     }  
  55.                 }  
  56.                 if (isInSection == true) {  
  57.                     strLine = strLine.trim();  
  58.                     String[] strArray = strLine.split("=");  
  59.                     if (strArray.length == 1) {  
  60.                         value = strArray[0].trim();  
  61.                         if (value.equalsIgnoreCase(variable)) {  
  62.                             value = "";  
  63.                             return value;  
  64.                         }  
  65.                     } else if (strArray.length == 2) {  
  66.                         value = strArray[0].trim();  
  67.                         if (value.equalsIgnoreCase(variable)) {  
  68.                             value = strArray[1].trim();  
  69.                             return value;  
  70.                         }  
  71.                     } else if (strArray.length > 2) {  
  72.                         value = strArray[0].trim();  
  73.                         if (value.equalsIgnoreCase(variable)) {  
  74.                             value = strLine.substring(strLine.indexOf("=") + 1)  
  75.                                     .trim();  
  76.                             return value;  
  77.                         }  
  78.                     }  
  79.                 }  
  80.             }  
  81.         } finally {  
  82.             bufferedReader.close();  
  83.         }  
  84.         return defaultValue;  
  85.     }  
  86.   
  87.     /** 
  88.      * 修改ini配置文档中变量的值 
  89.      *  
  90.      * @param file 
  91.      *            配置文档的路径 
  92.      * @param section 
  93.      *            要修改的变量所在段名称 
  94.      * @param variable 
  95.      *            要修改的变量名称 
  96.      * @param value 
  97.      *            变量的新值 
  98.      * @throws IOException 
  99.      *             抛出文档操作可能出现的io异常 
  100.      */  
  101.     public static boolean setProfileString(String file, String section,  
  102.             String variable, String value) throws IOException {  
  103.         String fileContent, allLine, strLine, newLine, remarkStr;  
  104.         String getValue;  
  105.         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
  106.         boolean isInSection = false;  
  107.         fileContent = "";  
  108.         try {  
  109.   
  110.             while ((allLine = bufferedReader.readLine()) != null) {  
  111.                 allLine = allLine.trim();  
  112.                 System.out.println("allLine == " + allLine);  
  113.                 // if (allLine.split("[;]").length > 1)  
  114.                 // remarkStr = ";" + allLine.split(";")[1];  
  115.                 // else  
  116.                 // remarkStr = "";  
  117.                 // strLine = allLine.split(";")[0];  
  118.                 strLine = allLine;  
  119.                 Pattern p;  
  120.                 Matcher m;  
  121.                 p = Pattern.compile("[url=file://[.*//]\\[.*\\]");  
  122.                 m = p.matcher((strLine));  
  123.                 if (m.matches()) {  
  124.                     p = Pattern.compile("[url=file://[(.*)//]\\[(.*)\\]");  
  125.                     m = p.matcher(strLine);  
  126.                     if (m.matches()) {  
  127.                         isInSection = true;  
  128.                     } else {  
  129.                         isInSection = false;  
  130.                     }  
  131.                 }  
  132.                 if (isInSection == true) {  
  133.                     strLine = strLine.trim();  
  134.                     String[] strArray = strLine.split("=");  
  135.                     getValue = strArray[0].trim();  
  136.                     if (getValue.equalsIgnoreCase(variable)) {  
  137.                         // newLine = getValue + " = " + value + " " + remarkStr;  
  138.                         newLine = getValue + " = " + value + " ";  
  139.                         fileContent += newLine + "\r\n";  
  140.                         while ((allLine = bufferedReader.readLine()) != null) {  
  141.                             fileContent += allLine + "\r\n";  
  142.                         }  
  143.                         bufferedReader.close();  
  144.                         BufferedWriter bufferedWriter = new BufferedWriter(  
  145.                                 new FileWriter(file, false));  
  146.                         bufferedWriter.write(fileContent);  
  147.                         bufferedWriter.flush();  
  148.                         bufferedWriter.close();  
  149.   
  150.                         return true;  
  151.                     }  
  152.                 }  
  153.                 fileContent += allLine + "\r\n";  
  154.             }  
  155.         } catch (IOException ex) {  
  156.             throw ex;  
  157.         } finally {  
  158.             bufferedReader.close();  
  159.         }  
  160.         return false;  
  161.     }  
  162.   
  163.     /** 
  164.      * 程式测试 
  165.      */  
  166.     public static void main(String[] args) {  
  167.         // String value = Config.getProfileString("sysconfig.ini", "Option",  
  168.         // "OracleDB", "default");  
  169.         // System.out.println(value);  
  170.         try {  
  171.             System.out.println("值已经改变!... "  
  172.                     + ConfigurationFile.setProfileString(  
  173.                             "F:/update/gamewww.ini""TestSect1""1001",  
  174.                             "111111"));  
  175.             System.out.println("值读取成功!... "  
  176.                     + ConfigurationFile.getProfileString(  
  177.                             "F:/update/gamewww.ini""TestSect1""1001"""));  
  178.         } catch (IOException e) {  
  179.             System.out.println("错误 ......" + e.toString());  
  180.         }  
  181.     }  
  182. }  


配置文件格式:

[TestSect1]
1001 = 111111
1002 = 222222
1003 = 333333  
1004 = 444444
1005 = 555555  
1006 = 666666
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值