Java properties in the use of detailed _java

Source: Internet
Author: User

Java has a more important class Properties (Java.util.Properties), mainly used to read Java configuration files, various languages have their own supported profiles, many of the variables in the configuration file are often changed, and this is done to facilitate the user, Allows users to change the relevant variable settings from the program itself. Today, we start using properties.

The use of properties in Java

Document Description of properties:

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from stream. Each key and its corresponding value in the property list is a string.

Description of Properties Class:

public class Properties extends hashtable<object,object>

The project structure of the test is as follows:

First, in the Huhx.properties file, we are also convenient to add a piece of data:

Name=huhx

Second, the Huhx.properties file load read, get the corresponding properties

Properties Properties = new properties ();
FileInputStream fis = new FileInputStream ("Huhx.properties");
Properties.load (FIS);

Third, the use of the list method of properties

PrintStream printstream = System.out;

Specific code for the list method:

public void list (PrintStream out) {
out.println ("--listing Properties-");
Hashtable h = new Hashtable ();
Enumerate (h);
For (Enumeration E = H.keys (); e.hasmoreelements ();) {
String key = (string) e.nextelement ();
String val = (string) h.get (key);
if (Val.length () >) {
val = val.substring (0, Panax) + "...";
}
OUT.PRINTLN (key + "=" + val);
}

Use of the store method of properties

OutputStream outputstream = new FileOutputStream ("Huhx.txt");

V. The use of Storetoxml method of properties

OutputStream outputStream2 = new FileOutputStream ("Huhx.xml");

Six, the final generation of documents are as follows:

Huhx.txt:

#comments
#Thu may 19:19:36 CST 2016

Huhx.xml:

<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> <!
DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >
<properties>
<comment >comments</comment>
<entry key= "name" >huhx</entry>

Friendship links, Propertiestest.java:

Package com.huhx.linux;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.io.PrintStream;
Import java.util.Properties;
public class Propertiestest {public
static void Main (string[] args) throws Exception {
//General properties Usage
Pr Operties Properties = new properties ();
FileInputStream fis = new FileInputStream ("Huhx.properties");
Properties.load (FIS);
System.out.println (Properties.get ("name"));
The following is part of the test
printstream printstream = System.out;
Properties.list (PrintStream);
OutputStream outputstream = new FileOutputStream ("Huhx.txt");
Properties.store (OutputStream, "comments");
OutputStream outputStream2 = new FileOutputStream ("Huhx.xml");
Properties.storetoxml (outputStream2, "comments");
}

The above is a small set to introduce the Java properties of the use of detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.