.ini 格式的程序配置文件中使用到的

从网上找的,读取文件,改变键值对的方法。




  1. package com.sdut.edu.tools;
  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.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. public class TestIni {
  10. private static String file="/sdcard/test.ini";
  11. public static String getProfileString(
  12. String section,
  13. String variable,
  14. String defaultValue)
  15. throws IOException {
  16. String strLine, value = "";
  17. BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  18. boolean isInSection = false;
  19. try {
  20. while ((strLine = bufferedReader.readLine()) != null) {
  21. strLine = strLine.trim();
  22. //strLine = strLine.split("[;]")[0];
  23. Pattern p;
  24. Matcher m;
  25. p = Pattern.compile("\\["+section+"\\]");
  26. m = p.matcher((strLine));
  27. if (m.matches()) {
  28. p = Pattern.compile("\\["+section+"\\]");
  29. m = p.matcher(strLine);
  30. if (m.matches()) {
  31. isInSection = true;
  32. } else {
  33. isInSection = false;
  34. }
  35. }
  36. if (isInSection == true) {
  37. strLine = strLine.trim();
  38. String[] strArray = strLine.split("=");
  39. if (strArray.length == 1) {
  40. value = strArray[0].trim();
  41. if (value.equalsIgnoreCase(variable)) {
  42. value = "";
  43. break;
  44. // return value;
  45. }
  46. } else if (strArray.length == 2) {
  47. value = strArray[0].trim();
  48. if (value.equalsIgnoreCase(variable)) {
  49. value = strArray[1].trim();
  50. break;
  51. // return value;
  52. }
  53. } else if (strArray.length > 2) {
  54. value = strArray[0].trim();
  55. if (value.equalsIgnoreCase(variable)) {
  56. value = strLine.substring(strLine.indexOf("=") + 1).trim();
  57. break;
  58. // return value;
  59. }
  60. }
  61. }
  62. System.out.println("");
  63. }
  64. } finally {
  65. bufferedReader.close();
  66. }
  67. System.out.println("value=====" +value);
  68. return value;
  69. }
  70. public static boolean setProfileString(
  71. String section,
  72. String variable,
  73. String value)
  74. throws IOException {
  75. String fileContent, allLine,strLine, newLine, remarkStr;
  76. String getValue;
  77. BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  78. boolean isInSection = false;
  79. fileContent = "";
  80. try {
  81. while ((allLine = bufferedReader.readLine()) != null) {
  82. allLine = allLine.trim();
  83. // System.out.println("allLine == "+allLine);
  84. strLine = allLine;
  85. Pattern p;
  86. Matcher m;
  87. p = Pattern.compile("\\["+section+"\\]");
  88. m = p.matcher((strLine));
  89. if (m.matches()) {
  90. // System.out.println("+++++++ ");
  91. p = Pattern.compile("\\["+section+"\\]");
  92. m = p.matcher(strLine);
  93. if (m.matches()) {
  94. // System.out.println("true ");
  95. isInSection = true;
  96. } else {
  97. isInSection = false;
  98. // System.out.println("+++++++ ");
  99. }
  100. }
  101. if (isInSection == true) {
  102. strLine = strLine.trim();
  103. String[] strArray = strLine.split("=");
  104. getValue = strArray[0].trim();
  105. if (getValue.equalsIgnoreCase(variable)) {
  106. // newLine = getValue + " = " + value + " " + remarkStr;
  107. newLine = getValue + " = " + value + " ";
  108. fileContent += newLine + "\r\n";
  109. while ((allLine = bufferedReader.readLine()) != null) {
  110. fileContent += allLine + "\r\n";
  111. }
  112. bufferedReader.close();
  113. BufferedWriter bufferedWriter =
  114. new BufferedWriter(new FileWriter(file, false));
  115. bufferedWriter.write(fileContent);
  116. bufferedWriter.flush();
  117. bufferedWriter.close();
  118. return true;
  119. }
  120. }
  121. fileContent += allLine + "\r\n";
  122. }
  123. }catch(IOException ex){
  124. throw ex;
  125. } finally {
  126. bufferedReader.close();
  127. }
  128. return false;
  129. }
  130. }
复制代码