Properties class for Java

Source: Internet
Author: User
Tags list of attributes

Java provides a properties class under the Util package to manipulate the properties resource file:

 Public class extends Hashtable<object, object>

It inherits Hashtable<object, Object> is an indirect subclass of dictionary<k,v>.

At the same time, it realizes the serializable, Cloneable, Map<object, object> interface.

The direct subclass is provider

The properties class represents the persistence of properties, which are usually temporary in a program. If we need to keep the attribute in place for a long time, we need to store the attribute in a database or file, which is used to persist the attribute in the properties file.

Properties can be stored in a stream or loaded from a stream, and each key value in the attribute list is a string.

It implements the map interface, and of course it can use put, but it is not recommended to use put, because the method allows its values to be non-character-shaped, so for the sake of security, we recommend using the SetProperty method, so that at compile time you can check the value is correct.

If it is an XML file format to store the Properties property, you need to declare

<! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >

For example:

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><!DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd"><Properties><entryKey= "Age">40</entry><entryKey= "Name">Lay</entry></Properties>

API See:

Method Summary
 String getProperty(String key)
Searches for a property in this property list with the specified key.
 String getProperty(String key, String defaultValue)
Searches for a property in the property list with the specified key.
 void list(PrintStream out)
Outputs a list of attributes to the specified output stream.
 void list(PrintWriter out)
Outputs a list of attributes to the specified output stream.
 void load(InputStream inStream)
Reads the list of attributes (key and element pairs) from the input stream.
 void load(Reader reader)
Reads the list of attributes (key and element pairs) from the input character stream in a simple line-oriented format.
 void loadFromXML(InputStream in)
Loads all the attributes in the specified input stream that are represented by the XML document into this property sheet.
 Enumeration<?> propertyNames()
Returns an enumeration of all the keys in a property list, including a different key in the default attribute list if a key with the same name is not found in the main attribute list.
 void save(OutputStream out, String comments)
is obsolete. If an I/O error occurs while saving the property list, this method does not throw IOException. The preferred way to save a property list is through a store(OutputStream out, String comments) method or storeToXML(OutputStream os, String comment) method.
 Object setProperty(String key, String value)
Call the Hashtable method put .
 void store(OutputStream out, String comments)
load(InputStream) Properties writes the list of Properties attributes (key and element pairs) in this table to the output stream in a format that is appropriate for the use of the method to be loaded into the table.
 void store(Writer writer, String comments)
load(Reader) Properties writes the list of attributes (key and element pairs) in this table to the output character in a format that is appropriate for using the method.
 void storeToXML(OutputStream os, String comment)
Emits an XML document that represents all the attributes contained in this table.
 void storeToXML(OutputStream os, String comment, String encoding)
Emits an XML document that represents all the attributes contained in this table, using the specified encoding.
 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.
//individual methods using the sample
Public classTest { Public Static voidMain (string[] args) {InputStream InputStream=NULL; //read from XMLInputStream = Test.class. getResourceAsStream ("/filexml.xml"); Properties User=NewProperties (); Try{user.loadfromxml (InputStream); User.setproperty ("Age", "40"); System.out.println (User.getproperty ("Name")); System.out.println (User.getproperty ("Age")); //Get Project PathString path = Test.class. GetResource ("/"). GetPath (); SYSTEM.OUT.PRINTLN (path); //output is XML, default is Utf-8User.storetoxml (NewFileOutputStream (path + "/file.xml"), "comment content"); User.list (System.out); } Catch(IOException e) {e.printstacktrace (); } }}

Properties class for Java

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.