Set the ini configuration file in Java

Source: Internet
Author: User

In Java, configuration files are generally in two forms: XML files or property files. However, most people are used to using INI files, and the INI file's segmentation and annotation functions are easier to understand than XML. The class library in VC contains standard functions for reading and writing INI files. In dephi or other languages, you can also use Windows API functions to read and write INI files. However, there seems to be no ready-made classes and methods available in Java. Although Java can call windows APIS by loading DLL files, it is not authentic. So I wrote a class to read and write the ini configuration file 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 the ini configuration file.
* @ Author by month
* @ Version 2004-08-18
*/
Public final class configurationfile {
/**
* Read the variable value from the ini configuration file
* @ Param file: Path of the configuration file
* @ Param section name of the variable to be obtained
* @ Param variable: name of the variable to be obtained
* @ Param defaultvalue the default value when the variable name does not exist
* @ Return variable value
* @ Throws ioexception throws Io exceptions that may occur during file operations
*/
Public static string getprofilestring (
String file,
String section,
String variable,
String defaultvalue)
Throws ioexception {
String strline, value = "";
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 *" + section + "// s * //]");
M = P. matcher (strline );
If (M. Matches ()){
Isinsection = true;
} Else {
Isinsection = false;
}
}
If (isinsection = true ){
Strline = strline. Trim ();
String [] strarray = strline. Split ("= ");
If (strarray. Length = 1 ){
Value = strarray [0]. Trim ();
If (value. inclusignorecase (variable )){
Value = "";
Return value;
}
} Else if (strarray. Length = 2 ){
Value = strarray [0]. Trim ();
If (value. inclusignorecase (variable )){
Value = strarray [1]. Trim ();
Return value;
}
} Else if (strarray. length> 2 ){
Value = strarray [0]. Trim ();
If (value. inclusignorecase (variable )){
Value = strline. substring (strline. indexof ("=") + 1). Trim ();
Return value;
}
}
}
}
} Finally {
Bufferedreader. Close ();
}
Return defaultvalue;
}
/**
* Modify the variable value in the ini configuration file.
* @ Param file: Path of the configuration file
* @ Param section name of the variable to be modified
* @ Param variable: variable name to be modified
* @ Param value new value of the Variable
* @ Throws ioexception throws Io exceptions that may occur during file operations
*/
Public static Boolean setprofilestring (
String file,
String section,
String variable,
String Value)
Throws ioexception {
String filecontent, allline, strline, newline, remarkstr;
String getvalue;
Bufferedreader = new bufferedreader (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 (M. Matches ()){
Isinsection = true;
} Else {
Isinsection = false;
}
}
If (isinsection = true ){
Strline = strline. Trim ();
String [] strarray = strline. Split ("= ");
Getvalue = strarray [0]. Trim ();
If (getvalue. inclusignorecase (variable )){
Newline = getvalue + "=" + value + "" + remarkstr;
Filecontent + = newline + "/R/N ";
While (allline = bufferedreader. Readline ())! = NULL ){
Filecontent + = allline + "/R/N ";
}
Bufferedreader. Close ();
Bufferedwriter =
New bufferedwriter (New filewriter (file, false ));
Bufferedwriter. Write (filecontent );
Bufferedwriter. Flush ();
Bufferedwriter. Close (); Return true;
}
}
Filecontent + = allline + "/R/N ";
}
} Catch (ioexception ex ){
Throw ex;
} Finally {
Bufferedreader. Close ();
}
Return false;
}
/**
* Program testing
*/
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 the INI file, but first note: It recognizes ";" in the INI file as the annotator rather than "#" as the annotator.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.