Java read-Write properties configuration file method

Source: Internet
Author: User
Tags finally block

1. Properties Class

The Properties class represents a persistent set of attributes. Properties can be saved in a stream or loaded from a stream, and key and value in the attribute list must be strings.

Although the properties class inherits java.util.Hashtable, you can use methods such as the put of Hashtable, but these methods allow data that is not of type string to result in unsafe behavior, so you should still use the setProperty method.

2. Main methods

Load (InputStream in) reads the list of properties from the input stream

GetProperties (string key) Gets the value of the specified key, returning a string

SetProperty (string key, String value) Sets or modifies the value of a property

Store (OutputStream out, String comments) writes the properties object to an output stream, comments as a comment, and comments empty without comment

The following code demonstrates

1 /*Initial configuration file2 aa=13 bb=24 cc=35 */6 7Properties prop =NewProperties ();//Create a Properties object8InputStream in =NULL;9FileOutputStream ofile =NULL;Ten Try { Onein =NewFileInputStream ("FilePath");//Create an input stream file object AProp.load (in);//Load Input stream -System.out.println ("aa=" + prop.getproperty ("AA"));//aa=1 -Prop.setproperty ("AA", "11");//Modify the value of "AA" theOfile =NewFileOutputStream ("FilePath");//creating an output stream file object -Prop.store (Ofile, "");//saves properties of the Property object to the output stream specified file -}Catch(IOException e) { - Log.error (e); +}finally { -     Try { +Ofile.close ();//turn off the output stream A}Catch(IOException e) { at Log.error (e); -     } -     Try { -In.close ();//close the input stream -}Catch(IOException e) { - Log.error (e); in     } -}

The final shutdown IO stream is important, and must be performed in the finally block of code.

3. Some problems when modifying the properties configuration file

Reading the configuration file is generally not a problem, the time to modify and write a little more complex, the problems encountered in the record

3.1 Configure the second parameter of FileOutputStream true, which causes the configuration file to be repeatedly appended to the end of the configuration item

FileOutputStream constructor function

FileOutputStream (String name, Boolean append)

Append indicates whether to write to the file as a stream of characters appended to the end of the file, false by default, which is re-written to the configuration

The second parameter of the output stream configuration here is true, which causes the repeated configuration to be appended to the end of the configuration file, resulting in an increasing size of the file specified by the output stream. So it's best not to add this parameter

1 /*Initial configuration file2 aa=13 bb=24 cc=35 */6 7Properties prop =NewProperties (); 8InputStream in =NewFileInputStream ("FilePath"); 9 prop.load (in); TenProp.setproperty ("AA", "11");  OneFileOutputStream ofile =NewFileOutputStream ("FilePath",true); AProp.store (ofile, "");  -  - / * After executionconfiguration file the aa=1 - bb=2 - cc=3 -  + aa=11 - bb=2 + cc=3 A */
3.2 FileOutputStream Creating a location leads to weird things

is mainly relative to the SetProperty () method.

The normal is to SetProperty () set the property and then create the FileOutputStream object

1 /*Initial configuration file2 aa=13 bb=24 cc=35 */6 7 //normal wording8InputStream in =NewFileInputStream ("FilePath"); 9 prop.load (in);TenProp.setproperty ("AA", "11");  OneFileOutputStream ofile =NewFileOutputStream ("FilePath");  AProp.store (ofile, "");  -  - //question wording theInputStream in =NewFileInputStream ("FilePath");  -FileOutputStream ofile =NewFileOutputStream ("FilePath"); Create in advance - prop.load (in); -Prop.setproperty ("AA", "11");  +Prop.store (ofile, "");  -  + /*configuration file after normal execution A aa=11 at bb=2 - cc=3 - */ -  - /*configuration files After the issue is executed - aa=11 in */

If this is reversed, it causes the property to be modified except for setproperty (), and the others are lost.

Did not want to understand this is why, some people understand can be pointed.

3.3 Change the file content order after reading and modifying the properties file

After reading and modifying a configuration file using the properties class provided by the JDK, the order that is loaded into memory is random and does not guarantee the same order as the original file, so you need to override the properties class to implement sequential read of the Properties property.

Java read-Write properties configuration file method

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.