In Java, the configuration file is generally two forms: XML file or Property file. But most people are used to using the INI file, and the component of the INI file and the comment function, which is easy to use. There is a standard function of reading the INI file in the library library in the VC. In DEPHI or other languages, you can also read the INI file with a Windows API function. However, there is no ready-made class and method in Java. Although Java can call Windows API by loading a DLL file, it always feels not authentic enough. So I wrote a class read and write the INI profile for your reference.
package mytools; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java. Util.regex.pattern; / ** * This is a configuration file operation class, used to read and set INI profile * @author by month * @version 2004-08-18 * / public final class configurationFile {/ ** * Value from the INI profile * @Param file profile path * @Param section To get the variable where the variable to get * @Param variable To get the variable name * @Param defaultValue variable name does not exist when there is no default value * the value of the variable @return * @throws IOException thrown io file operation may appear abnormal * / public static String getProfileString (String file, String section, String variable, String defaultValue) throws IOException {String strLine, value = ""; BufferedReader bufferedReader = new BufferedReader (new FileReader (file)); boolean isInSection = false; try {while (! (strLine = bufferedReader.readLine ()) = null) {strLine = strLine.trim (); strLine = strLine.split ( " [;] ") [0]; pattern p; matcher m; p = pattern.compile (" //// s *. * // s * //] "); m = p.matcher ((strline)); if (m.matches ()) {p = pattern.Compile ("//// s *" "// s * //]"); m = P.Matcher (Strline); if (matches ()) {isInsection = true;} else {isInsection = false;}}} == = f (isInsection == true) {strline = strline.trim (); string [] strartray = Strline.Split ("="); if (strhao.length == 1) {value = stratay [0] .trim (); if (Value.Equalsignorecase (variable)) {value = ""; return value;}} Else IF (strARRAY.LENGTH == 2) {Value =
Strarray [0] .trim (); if (Value.Equalsignorecase (variable)) {value = stratay [1] .trim (); return value;}} else if (strhao.length> 2) {value = strarth> 2) {value = stratay [0 ] .trim (); if (Value.equalsignoreCase (variable) {value = strline.substring (strline.indexof ("=") 1) .trim (); return value;}}}}} finally}}}}} finally}}}}} finally}}}}} Close ();} Return DefaultValue;} / ** * Modify the value of the variable in the INI profile * @Param file profile path * @Param section To modify the variable of the variable to be modified * @Param variable To modify the variable name * @param value the new value of the variable * @throws IOException thrown io file operations possible exception * / public static boolean setProfileString (String file, String section, String variable, String value) throws IOException {String fileContent, allLine, strLine, newLine , remarkStr; String getValue; BufferedReader bufferedReader = new BufferedReader (new FileReader (file)); boolean isInSection = false; fileContent = ""; try {while (! (allLine = bufferedReader.readLine ()) = null) {allLine = allLine .trim (); IF ("[;]"). Length> 1) Remarkstr = ";" allline.split (";") [1]; else remarkstr = "; strline = allline.split ("; " ) [0]; Pattern P; Matcher M; P = Pattern.Compile ("//// s *. * ////////]"); m = p.matcher ((strline)); if ( m.matches ()) {p = pattern.compile ("// [// s *" section "// s * //]"); m = p.matcher (strline); if (mtches ()) {IsInsection = true;} else {isInsECTION = false;}}}} {strline = strline.trim ();
String [] stratay = strline.split ("="); getValue = stratay [0] .trim (); if (getValue.equalsignorecase (variable)) {newline = getValue "=" value "" Remarkstr; FileContent = newline "/ r / n"; while ((allline = bufferedreader.readline ())! = null) {filecontent = allline "/ r / n";} bufferedReader.close (); bufferedWriter BufferedWriter = New bufferedWriter (new filewriter (file, false); bufferedwriter.clush (); bufferedwriter.close (); bufferedwriter.close (); return true;}} filecontent = allline "/ r / n";}} Catch (ooException ex) {throw ex;} finally {buffredreader.close ();} Return false;} / ** * program test * / public static void main (string "args) {// string value = config.getProfileString ("sysconfig.ini", "option", "oracledb", "default"); //system.out.println (Value); try {system.out.println (ConfigurationFile.SetProfileString ("D: /1.ini "" Settings "," Sampsize "," 111 ")); } catch (ioException e) {system.out.println (e.tostring ());}}} This class can read and write ini files, but you will explain a little: it identifies ";" in the INI file. Instead of identifying "#" as an comment. Author blog: http://blog.9cbs.net/youyue/