Java.util.Properties description.
This class is mainly read property configuration file, two file types: Normal file format is key = Value;xml file.
1) Key = Value example is as follows:
public class Testproperties {public static void main (string[] args) {Properties Properties = new Properties (); FileInputStream fileinputstream;try {fileinputstream = new FileInputStream (New File ("D:/testproperties.txt"));// Properties.loadfromxml (FileInputStream);p roperties.load (FileInputStream); for (Object Pro:properties.keySet ()) { System.out.println (pro + "= =" + properties.get (PRO));}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
The configuration file is:
Name = Hellovalue = Good
The result is:
Name==hellovalue==good
2) Read the XML file with the following code:
public class Testproperties {public static void main (string[] args) {Properties Properties = new Properties (); FileInputStream fileinputstream;try {fileinputstream = new FileInputStream (New File ("D:/testproperties.xml")); Properties.loadfromxml (FileInputStream);//properties.load (FileInputStream); for (Object Pro:properties.keySet ()) { System.out.println (pro + "= =" + properties.get (PRO));}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
The configuration file is:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" ><properties><comment>hi</ Comment><entry key= "foo" >bar</entry><entry key= "Fu" >baz</entry><entry key= "Feee" >baeee</entry></properties>
The result of the operation is:
Feee==baeeefu==bazfoo==bar
2. Source Code Analysis:
The class inherits from Hashtable, and the structure is Key-value, as follows:
public class Properties extends hashtable<object,object>
1) Load (InputStream) method, the main function of this method is to read the configuration file within the relevant configuration, and the information with the Key-value structure into the properties. The source code is:
public synchronized void load (InputStream instream) throws ioexception { load0 (new linereader (instream)); }
private void load0 (LINEREADER&NBSP;LR) throws IOException { char[] convtBuf = new char[1024]; int limit; int keyLen; int valueStart; char c; boolean hassep; &NBSP;&NBSP;&NBSP;BOOLEAN&NBSP;PRECEDINGBACKSLASH;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1. Reading a configuration file by line , the return value is the length of the line while ((Limit = lr.readline ()) >= 0) { c = 0; keyLen = 0; valueStart = limit; hasSep = false; //system.out.println ("line=<" + new string (lineBuf, 0, limit) + ">"); precedingbackslash = false; / /1.1 traverse each character of the line to find = or: position of the symbol while (Keylen < limit) { c = lr.lineBuf[keyLen]; //need check if escaped. if ((c == ' = ' | | c == ': ') && !precedingbackslash { valuestart = keyLen + 1; hasSep = true; break; } else if ( (c == ' ' | | c == ' t ' | | c == ' \f ' && !precedingbackslash) { valuestart = keyLen + 1; break; } if (c == ' \ \ ') { precedingbackslash = ! precedingbackslash; } else { precedingBackslash = false; } keyLen++; } while (Valuestart < limit) { c = lr.linebuf[valuestart]; if (c != ' ' && c != ' \ t ' && c != ' \f ') { if (!hassep && (c == ' = ' | | c == ': ') { hasSep = true; } else { break; } } valueStart++; } //keylen the final position before the symbol is not empty, Valuestart is the first non-empty position after the symbol String Key = loadconvert (LR.LINEBUF,&NBSP;0,&NBSP;KEYLEN,&NBSP;CONVTBUF); string value = loadconvert (lr.linebuf, valuestart, limit - VALUESTART,&NBSP;CONVTBUF); // The parsed key and value are stored in the Hashtable put (key, value); } }
2) Store (OutputStream out, String comments) method, out for the output of the document, comments as the description of the configuration file, in Key-value as comment information, in the XML file format for < Comments> label content. The source code is as follows:
public void store (outputstream out, string comments) throws IOException { store0 (New bufferedwriter (New outputstreamwriter (out, "8859_1")), comments, true); } &NBSP;&NBSP;PRIVATE&NBSP;VOID&NBSP;STORE0 (Bufferedwriter bw, string comments, boolean escunicode) throws IOException { if (comments != null) { writecomments (bw, comments); } bw.write ("#" + new date (). toString ()); bw.newline (); synchronized (This) { for (Enumeration<?> e = keys (); e.hasmoreelements ();) { String key = ( String) e.nextelement (); String val = (String) Get (key); key = saveconvert (Key, true, escUnicode); /* no need to escape embedded and trailing spaces for value, hence * pass false to flag. */ val = Saveconvert (Val, false, escunicode); bw.write (key + "=" + val); bw.newline (); } } bw.flush (); } private static&nBsp;void writecomments (bufferedwriter bw, string comments) throws ioexception { bw.write ("#"); int len = comments.length (); int current = 0; Int last = 0; char[] uu = new char[6]; uu[0] = ' \ \ '; uu[1] = ' u '; while ( Current < len) { char c = comments.charat (current); if (c > ' \u00ff ' | | c == ' \ n ' | | c == ' \ R ') { if (last != current) bw.write (Comments.substring (Last, current)); if (c > ' \u00ff ') { uu[2] = tohex ((c >> 12) &NBSP;&&NBSP;0XF); uu[3] = tohex ((c >> 8) & 0xf ); uu[4] = tohex (c >> 4) & 0XF); uu[5] = tohex ( c & 0XF); bw.write (new string (UU)); } else { bw.newline (); if (c == ' \ r ' && current != len - 1 && comments.charat (current + 1) == ' \ n ') { current++; } if (current == len - 1 | | (Comments.charat (current + 1) != ' # ' && Comments.charat (current + 1) != '! ') bw.write ("#"); } last = current + 1; } current ++; } if ( Last != current) bw.write ( Comments.substring (last, current)); &nbsP; bw.newline (); }
JDK Source Learning (9)-java.util.Properties instance and source code