Java sequential Read and write Properties configuration file support Chinese not garbled
Java sequential read and write properties configuration file, Java default provides the properties API inherit HashMap, not sequential read-write.
From the Internet to check the data, sequential reading and writing code, such as,
Import Java.util.collections;import Java.util.enumeration;import java.util.linkedhashset;import Java.util.properties;import Java.util.set;public class Orderedproperties extends Properties {private static final long Serialversionuid = -4627607243846121965l; Private final linkedhashset<object> keys = new linkedhashset<object> (); Public enumeration<object> keys () {return collections.<object> enumeration (keys); } public object put (object key, Object value) {Keys.add (key); Return Super.put (key, value); } Public Synchronized object remove (object key) {Keys.remove (key); return Super.remove (key); } public set<object> KeySet () {return keys; } public set<string> Stringpropertynames () {set<string> Set = new linkedhashset<string> (); for (Object Key:this.keys) {set.add (String) key); } return set; }}
Import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.InputStreamReader; Import Java.io.outputstream;import Java.io.outputstreamwriter;import Java.util.properties;public class propertiestest {public static void main (string[] args) {String ReadFile = "d:/eclipseworkspace/test/src/test.t XT "; Properties Pro = Readpropertiesfileobj (ReadFile); Read the Properties file System.out.println (Pro.getproperty ("password0.9271224287974811")); Pro.remove ("password0.008229652622303574"); Writepropertiesfileobj (ReadFile, pro); Write properties file}//Read resource file and handle Chinese garbled public static Properties readpropertiesfileobj (String filename) {Pro Perties properties = new Orderedproperties (); try {inputstream InputStream = new FileInputStream (filename); BufferedReader bf = new BufferedReader (New InputStreamReader (InputStream, "utf-8")); Properties.load (BF); Inputstream.close (); Close stream} catch (IOException e) {e.printstacktrace (); } return properties; }//write resource file with Chinese public static void Writepropertiesfileobj (String filename, properties properties) {if (proper Ties = = null) {properties = new orderedproperties (); } try {OutputStream outputstream = new FileOutputStream (filename); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (OutputStream, "utf-8")); Properties.setproperty ("username" + math.random (), "myname"); Properties.setproperty ("Password" + math.random (), "MyPassword"); Properties.setproperty ("Chinese" + math.random (), "Chinese"); Properties.store (BW, NULL); Outputstream.close (); } catch (IOException e) {e.printstacktrace (); } }}
Java sequential Read and write Properties configuration file