Java Properties Basic Knowledge Summary

Source: Internet
Author: User
In the Java language, a text file with a. properties extension is used as a resource file with a similar content format for that type of file:
Some_key=some_value #注释描述
There is also the use of XML files to save the configuration information of the project, also can use the Properties class parsing.

XML configuration file Format:

<?xml version= "1.0" encoding= "UTF-8"?> <!  
DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >  
<properties>  
<comment> Hello world</comment>  
<entry key= "foo" >bar</entry>  
<entry key= "Fu" >baz</entry >  
</properties>


API Tutorial The Properties class represents a persistent set of attributes. Properties can be saved in the stream or loaded from the stream. Each key and its corresponding value in the attribute list is a string.

It provides several main methods:
1. GetProperty (String key)
Searches for properties in this property list with the specified key. That is, the value of the key is obtained by the key of the parameter.


2. Load (InputStream instream)
Reads the list of properties from the input stream (key and element pairs). Gets all key-value pairs in the file by loading the specified file, such as the Test.properties file above. To search for GetProperty (String key).


3. SetProperty (string key, String value)
Call the Hashtable method put. He sets the key-value pair by calling the base class's put method.


4. Store (OutputStream out, String comments)
Writes the list of properties in this properties table (key and element pairs) to the output stream in a format that is suitable for loading into the properties table using the Load method. In contrast to the load method, this method writes a key-value pair to the specified file.


5. Clear ()
Clears all loaded key-value pairs. This method is provided in the base class.


Application Examples

1, read the. properties file specifies the value code example of key for the following (Web project):

public static string GetProperties (String key) throws Exception {
	String value = "";
	InputStream InputStream = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Public.properties") ;
	Properties Properties = new properties ();
	Properties.load (InputStream);
	Value = Properties.getproperty (key);
	Inputstream.close ();
	return value;
}

2, read the XML configuration and read the KV configuration is not different, is the load into XML corresponding to the LoadFromXML method, the code is as follows:

public class Loadsamplexml {public
	static void Main (String args[]) throws Exception {
		Properties prop = new Prope Rties ();
		FileInputStream FIS =  new FileInputStream ("Sampleprops.xml");
		Prop.loadfromxml (FIS);
		Prop.list (System.out);
		System.out.println ("\nthe foo:" +  prop.getproperty ("foo"));
	}


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.