/*
Operating attribute files can bring more convenient portability to our programs. The following is an example of reading, writing, and modifying attributes.
Two read methods are used. One is the properties class, and the other is the resource binding class resourcebundle class,
The following is a source program with detailed notes:
*/
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. util. properties;
Import java. util. resourcebundle;
/**
* Operations on Attribute files (XX. properties)
* Note: The property file must be placed in the root directory of the current project, that is, in the same directory as the src directory (My jdevelop
* Yes)
*/
Public class operatepropertiesfile {
Public operatepropertiesfile (){
}
/**
* Use the properties class to obtain the corresponding value of the property file.
* @ Parampropertiesfilenameproperties file name, for example, A. Properties
* @ Parampropertyname: attribute name
* @ Return the attribute value obtained based on the attribute name. If no "" is returned ""
*/
Public static string getvaluebypropertyname (string propertiesfilename, string propertyname ){
String S = "";
Properties P = new properties (); // load the property File Read class
Fileinputstream in;
Try {
// Propertiesfilename, such as test. Properties
In = new fileinputstream (propertiesfilename); // read the attribute file as a stream
P. Load (in); // The attribute file adds the stream to the readable attribute
In. Close (); // close after reading
S = P. getproperty (propertyname); // gets the corresponding property value
} Catch (exception e ){
E. printstacktrace ();
}
Return S;
}
/**
* The resourcebundel class is used to obtain the corresponding value of the attribute file. This can only be read, and new attributes cannot be modified or written.
* @ Parampropertiesfilenamewithoutpostfixproperties file name without a suffix
* @ Parampropertyname: attribute name
* @ Return the attribute value obtained based on the attribute name. If no "" is returned ""
*/
Public static string getvaluebypropertyname _ (string propertiesfilenamewithoutpostfix, string propertyname ){
String S = "";
// If the property file is test. properties, the value of propertiesfilenamewithoutpostfix is test.
Resourcebundle Bundel = resourcebundle. getbundle (propertiesfilenamewithoutpostfix );
S = Bundel. getstring (propertyname );
Return S;
}
/**
* Change the value of the property file. If the corresponding property does not exist, the property is automatically added.
* @ Parampropertiesfilenameproperties file name, for example, A. Properties
* @ Parampropertyname: attribute name
* @ Parampropertyvalue: Change the attribute name to the attribute value.
* @ Return indicates whether the operation is successful.
*/
Public static Boolean changevaluebypropertyname (string propertiesfilename, string propertyname, string propertyvalue ){
Boolean writeok = true;
Properties P = new properties ();
Inputstream in;
Try {
In = new fileinputstream (propertiesfilename );
P. Load (in );//
In. Close ();
P. setproperty (propertyname, propertyvalue); // set the property value. If no property exists, create a property.
// P. setproperty ("testproperty", "testpropertyvalue ");
Fileoutputstream out = new fileoutputstream (propertiesfilename); // output stream
P. Store (Out, ""); // set the attribute header. If you do not want to set it, replace the following "".
Out. Flush (); // clears the cache and writes it to the disk.
Out. Close (); // close the output stream.
} Catch (exception e ){
E. printstacktrace ();
}
Return writeok;
}
}