Java Foundation----The use of properties in >java

Source: Internet
Author: User

Java has a relatively important class properties (Java.util.Properties), mainly used to read Java configuration files, various languages have their own supported configuration files, many variables in the configuration file is often changed, and this is also for the convenience of users, Allows the user to change the relevant variable settings from the program itself. Today, we begin to use the properties.

Use of properties in Java

Document Description of properties:

class Set  from inch  is string.

Description of the Properties class:

 Public class 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, to get the corresponding properties

Newnew fileinputstream ("huhx.properties");p roperties.load (FIS); System.out.println (Properties.get ("name"));

Third, the use of the properties of the list method

PrintStream PrintStream = system.out;properties.list (printstream);

Specific code for the list method:

 Public void list (PrintStream out) {    out.println ("--listing properties--");     New Hashtable ();    Enumerate (h);      for (Enumeration e = H.keys (); e.hasmoreelements ();) {        = (String) e.nextelement ();         = (String) h.get (key);         if (Val.length () >) {            = val.substring (0, PNS) + "...";        }         + "=" + val);}    }

Iv. use of the store method of the properties

New FileOutputStream ("Huhx.txt""comments");

V. Use of the Storetoxml method of the properties

New FileOutputStream ("Huhx.xml""comments");

Vi. the resulting files are as follows:

Huhx.txt:

19:19:36 CSTname=huhx

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>    <entryKey= "Name">Huhx</entry></Properties>

Links, Propertiestest.java:

 PackageCom.huhx.linux;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportJava.io.PrintStream;Importjava.util.Properties; Public classPropertiestest { Public Static voidMain (string[] args)throwsException {//use of general propertiesProperties Properties =NewProperties (); FileInputStream FIS=NewFileInputStream ("Huhx.properties");        Properties.load (FIS); System.out.println (Properties.get ("Name")); //here is the part of the testPrintStream PrintStream =System.out;        Properties.list (PrintStream); OutputStream OutputStream=NewFileOutputStream ("Huhx.txt"); Properties.store (OutputStream,"Comments"); OutputStream outputStream2=NewFileOutputStream ("Huhx.xml"); Properties.storetoxml (OUTPUTSTREAM2,"Comments"); }}
Propertiestest.java

Java Foundation----The use of properties in >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.