Java Read Properities file

Source: Internet
Author: User

Today read the Java properties of this east, usually at work, busy pinch.

Basic properties

1) Load (InputStream instream)

This method can load a property list into the Properties class object from the file input stream corresponding to the. Properties property file. as in the following code:

Newnew fileinputstream ("a.properties");p ro.load (in); In.close (); 

GetProperty (String key)

SetProperty (String key,string value)

Store (OutputStream out, String comments)

This method saves the property list of the properties class object to the output stream . as in the following code:

New FileOutputStream (file, True);p Ro.store (ofile, "Comment"); Comment can generate comments Ofile.close (); 

Publicclass propertytest {

public static void Main (string[] args) throws FileNotFoundException, IOException {

// TODO auto-generated Method Stub

Properties pro=New properties ();

FileInputStream f=New fileinputstream ("config.properties");

Pro.load (f);

//Set<string> stringpropertynames ()

Returns the set of keys in this property list, where the key and its corresponding value are strings, and if a key with the same name is not found in the main attribute list, the different keys in the default attribute list are also included.

Iterator<string> s=pro.stringpropertynames (). Iterator ();

while (S.hasnext ()) {

String Key=s.next ();

System. out . println (key +""+pro.getproperty (key));

}

F.close ();

The Save properties file is written to the output stream.

FileOutputStream ofile=New fileoutputstream ("b.properties",true );//indicates that the file can be appended.

Pro.setproperty ("Phone", "10086");

Pro.store (ofile,"The new Properties");//Generate a review

Ofile.close ();

}

Java Read Properities file

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.