Operations on Attribute files (XX. properties)

Source: Internet
Author: User

/*
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;
}
}

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.