Java operation. properties File

Source: Internet
Author: User

The properties file in Java is a configuration file mainly used to express configuration information. The file type is *. properties. The format is a text file. The content format of the file is "Key = value". In the properties file, you can use "#" for comments, the properties file is widely used in Java programming and is easy to operate.

I. Several important methods of the properties class

The properties class exists in the Java. util package and inherits from hashtable.

1.Getproperty(String
Key) use the specified key to search for the attribute in this attribute list, that is, find the value corresponding to the key through the parameter key. If not, return null.

2.Setproperty(String
Key, string value) CallHashtableOfputMethod. The returned value isHashtable
Callput. Adds a new key-value pair to The. properties file.

3.Load(Inputstream
Instream) read the attribute list (key and element pair) from the input stream ). Obtains all the keys in the file by loading the specified file. properties file.
-Value pairs. For
Getproperty(String key) to search.

4.Store(Outputstream
Out, string comments) for useLoad(Inputstream instream) method to load the format to the properties table, write the attribute list (key and element pair) in this properties table to the output stream. And
The load method writes the key-value pair to the specified file.

5.Clear()
Clear all mounted key-value pairs. This method is used in the base classHashtable <K, V>.

2. How to operate the. properties file in the program

Provide a complete code for your reference.

Package COM. properties; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import Java. util. enumeration; import Java. util. properties; public class testproperties {/*** read the value of value through key ** @ Param keyname * key name * @ Param filepath * file path to be read * @ return key corresponding value */public static object getkeyinfo (string filepath, string keyname) {inputstream is = testproperties. class. getclassloader (). getresourceasstream (filepath); properties pro = new properties (); try {pro. load (is);} catch (ioexception e) {e. printstacktrace ();} finally {try {is. close (); is = NULL;} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return pro. get (keyname );} /*** write key-value pairs to properties ** @ Param filepath * file path to be written * @ Param keyname * key value * @ Param value */public static void setproperties (string filepath, string keyname, string value) {inputstream is = testproperties. class. getclassloader (). getresourceasstream (filepath); outputstream OS = NULL; properties pro = new properties (); try {pro. load (is); OS = new fileoutputstream (filepath); pro. setproperty (keyname, value); pro. store (OS, "Update'" + keyname + "'value");} catch (ioexception e) {e. printstacktrace ();} finally {try {is. close (); OS. close (); is = NULL; OS = NULL;} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}/ *** read all key-value pairs in the properties file ** @ Param filepath * file path */public static void readproperties (string filepath) {properties props = new properties (); try {inputstream in = testproperties. class. getclassloader (). getresourceasstream (filepath); props. load (in); enumeration en = props. propertynames (); While (en. hasmoreelements () {string key = (string) en. nextelement (); string property = props. getproperty (key); system. out. println (Key + property) ;}} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {testproperties. getkeyinfo ("testpro. properties "," A "); testproperties. setproperties ("testpro. properties "," C "," 33333 "); readproperties (" src \ testpro. properties ");}}

Supplement: The. properties file is usually stored in the SRC root directory or you can create a directory under SRC to store the file. Of course, it can be stored elsewhere.

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.