Read and write INI profiles with Java

xiaoxiao2021-03-06  59

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 * @version 2004-08-18 * / public final class configurationFile {/ ** * read from the INI profile Take the value of the variable * @Param file configuration files * @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 * @return variable value * @throws IOException thrown io file operations possible exception * / 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 *. * ///////]"); m = p.matcher ((strline)); if (m. Matches ()) {p = pattern.Compile ("// [// s *" section "); m = p.matcher (strline); if (mtches () ) {IsInsection = true;} else {isinsection = false; }}} If (isInsection == True) {strline = strline.trim (); string [] stratay = strline.split ("="); if (strhao.length == 1) {value = stratay [0] .trim (); Value.equalsignorecase (variable) {value = ""; return value;}} else} else} {value = strangth == 2) {value = stratay [0] .trim (); if (Value.Equalsignorecase) (variable)) {value = stratay [1] .trim (); return value;}} else} else} else} {value = strRaRray [0] .trim ();

IF (Value.EqualsignoreCase (variable)) {value = strline.substring (strline.indexof ("=") 1) .trim (); return value;}}}}} finally}}}}} finally}}}}} finary DEFAULTVALUE;} / ** * Modify the value of the variable in the INI profile * @Param File configuration file path * @Param section To modify the variable where the variable is located * @Param Variable To modify the variable name * @Param value variable value * @throws IOException thrown file operations that may appear abnormal io * / 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 FileReader (New FileReader (File); Boolean isInsection = false; filecontent = ""; TRY {

While ((allLine = BufferedReader.Readline ())! = null) {AllLine = allline.trim (); if (allLine.Split ("[;]"). Length> 1) Remarkstr = ";" allline.split (";") [1]; else remarkstr = ""; strline = allline.split (";") [0]; pattern p; matcher m; p = pattern.Compile ("//// s *. * // s * //] "); m = p.matcher ((strline)); if (m.matches ()) {p = pattern.compile (" // [// s * " section " // s * //] "); m = p.matcher (strline); if (matches ()) {isInsection = true;} else {isInsection = false;}}}} }Insection == true) {Strline = strline.trim (); string [] stratay = strline.split ("="); getValue = stratay [0] .trim (); if (getValue.equalsignore (variable)) {newline = getValue "=" Value "" Remarkstr; FileContent = newline "/ r / n"; while ((allline = buffredreader.readline ())! = null) {filecontent = allline "/ r / n";} bufferedReader. Close (); bufferedWriter bufferedWriter = New BufferedWriter (New Filewriter (File, False); BufferedWriter.write (FileContent); BufferedWriter.Flush (); 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 first explain a little: it identifies ";" as an comment in the INI file, rather than identifying "#" as an comment.

转载请注明原文地址:https://www.9cbs.com/read-118655.html

New Post(0)