一个非常好用的Java读写INI配置文件的类

By Minidxer | November 17, 2008

由于一个老项目中需要用Java读写INI配置文件,找了一下,发现“由月”写的类相当的好用,省却了我不少事情,感谢“由月”,下面是代码:


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

Topics: 程序开发相关 | No Comments » | 1,154 views Tags: ,

Search Posts