ext.: http://www.2cto.com/px/201006/47834.html
Under the Java.util package, there is a class properties, which is primarily used to read the configuration file in the project (in. Properties end of the file and XML file). Properties have two constructors, one without parameters, and one with a properties object as parameters. Read with properties. The properties file Test.properties file is as follows: #测试环境配置: Platform path configuration Jstrd_home=d:/tms2006/webapp/tms2006/web-inf/Dbport=localhost databaseName=mydb Dbusername=Root Dbpassword=Root # The following is the database table information dbtable=MyTable # The following is the server information IP=192.168.0.9here's how to read test.properties: Impor java.io.*; Import Java.util.*; Public classReadProperties { Public Static voidMain (string[] args) {File pFile=NewFile ("e:\test.properties");//The properties file is placed under the E disk (Windows)FileInputStream Pinstream=NULL; Try{Pinstream=NewFileInputStream (PFile); } Catch(FileNotFoundException e) {e.printstacktrace ();//To change body of catch statement use File | Settings | File Templates.} Properties P=NewProperties (); Try{p. load (Pinstream); //the Properties object has been generated, including the data in the file}Catch(IOException e) {e.printstacktrace ();//To change body of catch statement use File | Settings | File Templates.} enumeration enu= P.propertynames ();//Take out all the keys//Output--1p.list (System. out) ;//System.out can be changed to other output streams (including output to file)//Output--2 while(ENU. hasmoreelements ()) {System. out. Print ("key="+enu.nextelement ()); System. out. Print ("value="+P.getproperty (String) enu. nextelement ()); Read the XML-formatted configuration file Test.xml file Ruxi<?xml version="1.0"encoding="UTF-8"?> <! DOCTYPE Properties SYSTEM"HTTP://JAVA.SUN.COM/DTD/PROPERTIES.DTD"> <properties> <entry key="Koo">bar</entry> <entry key="Fu">baz</entry> </properties>the method of reading XML import java.io.IOException; Import Java.io.File; Import Java.io.FileInputStream; Import java.util.Properties; Public classTest { Public Static voidMain (string[] args) {File pFile=NewFile ("E:\test.xml");//The properties file is placed under the E disk (Windows)FileInputStream Pinstream=NULL; Try{Pinstream=NewFileInputStream (PFile); Properties P=NewProperties (); P.loadfromxml (Pinstream); P.list (System. out); } Catch(IOException e) {e.printstacktrace (); }}} by using the list method to write properties to the properties file import java.io.IOException; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.PrintStream; Import java.util.Properties; Public classTest { Public Static voidMain (string[] args) {Properties P=NewProperties (); P.setproperty ("ID","Dean"); P.setproperty ("Password","123456"); Try{printstream FW=NewPrintStream (NewFile ("e:\test1.properties")); P.list (FW); } Catch(IOException e) {e.printstacktrace (); }}} is saved as XML import java.io.IOException; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.PrintStream; Import java.util.Properties; Public classTest { Public Static voidMain (string[] args) {Properties P=NewProperties (); P.setproperty ("ID","Dean"); P.setproperty ("Password","123456"); Try{printstream FW=NewPrintStream (NewFile ("E:\test1.xml")); P.storetoxml (FW,"Test"); } Catch(IOException e) {e.printstacktrace (); }}}
Java.util Properties Usage Record