Home >Backend Development >PHP Tutorial >Used in program configuration files in .ini format

Used in program configuration files in .ini format

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Socket usage relatedNext article:Socket usage related