Java sequential Read and write Properties configuration file

Source: Internet
Author: User
Tags readfile

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

Related Article

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.