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