/**
* 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 ();
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.