The Java Properties class is output in the input order, and each comment is added.

Source: Internet
Author: User

Today, I want to take a look at the information of the input Property class. After the output, I find that the order is messy. Then I can see the code to understand what is going on, so I decided to write one by myself, tested and usable. Hope you can observe and guide me .... [Java] import java. io. bufferedWriter; import java. io. IOException; import java. io. writer; import java. util. date; import java. util. iterator; import java. util. linkedHashMap; import java. util. properties;/*** This class is to provide function output properties by input order and add comment to each property. * @ author zhengfan1 */public class OutputOrderProperties extends Properties {private limit H AshMap <String, String> commentMap = new LinkedHashMap <String, String> ();/*** Version ID */private static final long serialVersionUID = 1L;/*** Constructor. */public OutputOrderProperties () {super ();}/*** Constructor. * @ param properties * the java propertis. */public OutputOrderProperties (Properties properties) {super (properties); // Initialize the comment. iterator <Object> iterator = pr Operties. keySet (). iterator (); while (iterator. hasNext () {Object key = iterator. next (); this. commentMap. put (String) key, null) ;}/ *** Add comment to a property. * @ param key * the key of the property. * @ param comment * the comment of the property. * @ return * true => add it * false => don't have this key. */public boolean addComment (String key, String comment) {if (this. contains (key) {th Is. commentMap. put (key, comment); return true;} return false;}/*** To set property. * @ param key * the key of property. * @ param value * the value of property. * @ param comment * the comment of property. */public void setP (String key, String value, String comment) {this. commentMap. put (key, comment); this. setProperty (key, value);}/*** To output according to the order of input. * @ param wr Iter * the writer * @ param comments * the comments of this property file. * @ throws IOException * exception. */public void orderStore (Writer writer, String comments) throws IOException {BufferedWriter bufferedWriter = (writer instanceof BufferedWriter )? (BufferedWriter) writer: new BufferedWriter (writer); if (comments! = Null) {OutputOrderProperties. writeComments (bufferedWriter, comments);} bufferedWriter. write ("#" + new Date (). toString (); bufferedWriter. newLine (); bufferedWriter. newLine (); synchronized (this) {Iterator <String> iterator = this. commentMap. keySet (). iterator (); while (iterator. hasNext () {String key = iterator. next (); String value = this. getProperty (key); String comment = this. commentMap. get (key ); Key = saveConvert (key, true, false); value = saveConvert (value, false, false); key = saveConvert (key, true, false); if (comment! = Null &&! Comment. equals ("") {writeComments (bufferedWriter, comment);} bufferedWriter. write (key + "=" + value); bufferedWriter. newLine (); bufferedWriter. newLine () ;}} bufferedWriter. flush ();}/** Converts unicodes to encoded \ uxxxx and escapes * special characters with a preceding slash *!!! Copy from java source code. */private String saveConvert (String theString, boolean escapeSpace, boolean escapeUnicode) {int len = theString. length (); int bufLen = len * 2; if (bufLen <0) {bufLen = Integer. MAX_VALUE;} StringBuffer outBuffer = new StringBuffer (bufLen); for (int x = 0; x <len; x ++) {char aChar = theString. charAt (x); // Handle common case first, selecting largest block that // avoid S the specials below if (aChar> 61) & (aChar <127) {if (aChar = '\') {outBuffer. append ('\'); outBuffer. append ('\'); continue;} outBuffer. append (aChar); continue;} switch (aChar) {case '': if (x = 0 | escapeSpace) outBuffer. append ('\'); outBuffer. append (''); break; case '\ t': outBuffer. append ('\'); outBuffer. append ('T'); break; case '\ N': outBuffer. append ('\'); outBuffer. append ('n' '); Break; case' \ R': outBuffer. append ('\'); outBuffer. append ('R'); break; case '\ F': outBuffer. append ('\'); outBuffer. append ('F'); break; case '=': // Fall through case '#': // Fall through case '! ': OutBuffer. append ('\'); outBuffer. append (aChar); break; default: if (aChar <0x0020) | (aChar> 0x007e) & escapeUnicode) {outBuffer. append ('\'); outBuffer. append ('U'); outBuffer. append (toHex (aChar> 12) & 0xF); outBuffer. append (toHex (aChar> 8) & 0xF); outBuffer. append (toHex (aChar> 4) & 0xF); outBuffer. append (toHex (aChar & 0xF);} else {outBuffer. append (aChar) ;}} return o UtBuffer. toString ();}/**!!! Copy from java source code. */private static 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) & 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); bw. newLine ();}/**!!! Copy from java source code. */private static char toHex (int nibble) {return hexDigit [(nibble & 0xF)];} www.2cto.com/** A table of hex digits */private static final char [] hexDigit = {'0', '1', '2', '3', '4 ', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F' };} [java] <p> </p> <p> mainly implements two functions: </p> <p> 1. ordered output: </p> <p> you can use setP to insert data. The three parameters are key, value, and comment. And then output the content through orderStore. </P> <p> 2. Add comments for each attribute </p> <p> the attributes added using the setP method have annotation parameters. If you only want to use the annotation function, you can use the java Properties class to construct such objects, and then use the addComment method to add comments to the attributes you want to add. </p> test program: [java] public static void main (String [] args) throws IOException {OutputOrderProperties properties = new OutputOrderProperties (); FileOutputStream fileOutputStream = new FileOutputStream ("test. properties "); OutputStreamWriter writer = new OutputStreamWriter (fileOutputStream); for (int I = 0; I <10; I ++) {String string = String. valueOf (I); properties. setP ("Name" + string, string, "the name of" + string);} properties. orderStore (writer, "This is a test process... ");} result: [plain] # This is a test process... # Wed Mar 13 15:34:03 CST 2013 # the name of 0 Name0 = 0 # the name of 1 Name1 = 1 # the name of 2 Name2 = 2 # the name of 3 Name3 = 3 # the name of 4 Name4 = 4 # the name of 5 Name5 = 5 # the name of 6 Name6 = 6 # the name of 7 Name7 = 7 # the name of 8 Name8 = 8 # the name of 9 Name9 = 9

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.