Java Get Properties Property File Sample _java

Source: Internet
Author: User

A list of properties can contain another list of attributes as its default value, and if a property key cannot be searched for in the existing list of properties, search for the second property list.

Because properties inherit from Hashtable, the put and Putall methods can be applied to properties objects. However, these two methods are not recommended because they allow the caller to insert an item whose key or value is not a String. Instead, you should use the SetProperty method. If the store or Save method is invoked on an "unsafe" Properties object (that is, a key or value that contains a non-String), the call fails. Similarly, if the PropertyNames or list method is invoked on an "unsafe" properties object (that is, a key that contains a non-String), the call will fail.

The Properties property file is a frequently visible and particularly important type of file in a Java application. It is used to configure some of the information of the application, but this information is generally relatively small data, there is no need to use the database file to save, and the use of ordinary text files to save, if it is directly saved by file, may be stored and read on is not very convenient, However, if you save as a properties file is not the same, the property file has a key value, in the Java package, there are special operations to provide the properties of the file class. This class is the Java.uitl.Properties class, and since the properties class is a collection class, properties will read and write the attributes as a collection.

Note: The following code does not capture the exception thrown in the way, you write the program must pay attention to catch the exception, the recommended to handle the caught exception.

Properties class inherits the character Hashtable class, uses the key value corresponding storage way, when uses the properties class to manage the attribute file to have what convenience? Properties class has a special reading and writing methods to read and write properties file, do not worry about the format of reading and writing, as long as the properties class to provide a read-write stream. Properties are used to read and write property files in the following ways:

Copy Code code as follows:

Ways to read the property file stream

public void Load (InputStream instream) throws IOException {}

How to write a property file stream

public void Store (OutputStream out, String comments) throws IOException {}

First, let's look at how to read properties from a property file.

Suppose we have created a new property file named Prop.properties, which reads as follows:

Copy Code code as follows:

Sitename=abcjava

Siteurl=www.abcjava.com

The first step is to read the file to the properties class object, because the load has a parameter is InputStream, so we can use InputStream subclasses FileInputStream Read the property file to the Properties object and know the prop.properties path, we use the FileInputStream (String name) constructor:

Copy Code code as follows:

Properties prop = new properties ();//Property Collection Object

FileInputStream fis = new FileInputStream ("prop.properties");//Property file stream

Prop.load (FIS);//Load the property file stream into the Properties object

After knowing how to read the properties file, we also have a very important thing to do is to modify and add new attributes to the property file, this is the use of the public void store (OutputStream out, String comments) method, This method is to write the collection of attributes into a outputstream stream, just like the InputStream stream, where it is also using its subclass FileOutputStream (String name), which is not much to say.

Before we save the property collection to a file, we have one more thing: How to modify and add a new attribute to a collection of attributes, where a method is SetProperty (string key, String value), which is when the specified key is present in the property collection. To modify the value of this key, if it does not exist, a new key, the same is saved through the key-value relationship, but it is noteworthy that the properties class inherits from Hashtable, so you can also use the Hashtable put and Putall methods to save, However, it is strongly opposed to the use of these two methods because they allow the caller to insert an item whose key or value is not Strings. Instead, you should use the SetProperty method. If the store or Save method is invoked on a Properties object that is in danger (that is, a key or value that contains a non String), the call will fail. Well, let's take a look at the program that modifies, adds, and saves properties:

Copy Code code as follows:

Modifying the SiteName property values

Prop.setproperty ("SiteName", "Boxcode");

Add a new property studio

Prop.setproperty ("Studio", "Boxcode studio");

File output stream

FileOutputStream fos = new FileOutputStream ("Prop.properties");

To save a Properties collection in a stream

Prop.store (FOS, "Copyright (c) boxcode Studio");

Fos.close ();//Close stream

Next is the entire program's source code:

Copy Code code as follows:

Import Java.io.FileInputStream;

Import Java.io.FileOutputStream;

Import java.util.Properties;

public class PropertyEditor {

public static void Main (string[] args) throws Exception {

Properties prop = new properties ();//Property Collection Object

FileInputStream fis = new FileInputStream ("prop.properties");//property file input stream

Prop.load (FIS);//Load the property file stream into the Properties object

Fis.close ();//Close stream

Gets the property value that SiteName already defined in the file

System.out.println ("Get Property Value: Sitename=" + prop.getproperty ("sitename"));

Gets the property value, country is not defined in the file, and returns a default value in this program, but does not modify the property file

SYSTEM.OUT.PRINTLN ("Get attribute value: country=" + prop.getproperty ("Country", "China"));

Modifying the SiteName property values

Prop.setproperty ("SiteName", "Boxcode");

Add a new property studio

Prop.setproperty ("Studio", "Boxcode studio");

File output stream

FileOutputStream fos = new FileOutputStream ("Prop.properties");

To save a Properties collection in a stream

Prop.store (FOS, "Copyright (c) boxcode Studio");

Fos.close ();//Close stream

}
}

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.