java Read and Write properties file java The most important class of read-write resource files is the properties, the functions are as follows: 1. Read and Write properties file 2. Read and write XML file 3. Not only can read and write the above two types of files, but also read and write other format files such as TXT, as long as the Key=value format can be . Note: The resource file contains Chinese when the processing Method 1. Convert Chinese characters through work to UTF8 encoding, either through Java's own nativetoascii or the property editor in Eclipse. 2. Call the new String directly (Youchinesestring.getbytes ("iso-8859-1"), "GBK"); attached: The method of loading the resource file in the Web program properties prop = Null;1. Prop = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("filename"); 2. Prop = This.getclass (). getClassLoader (). getResourceAsStream ("filename"), properties can read any format file stored in Key,value, What is magical, cat one eye class structure, Originally it inherits the Hashtable and realizes the map interface, so everybody rest assured of it. [java] View plaincopyprint?package apistudy; import java.io.file;import java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream;import Java.io.unsupportedencodingexception;import Java.util.properties; public class PropertiesTest{ public static void main (string[] args) { String ReadFile = "D:" + File.separator + "readfile.properties"; &NBS P String WriteFile = "D:" + File.separator + "writefile.properties"; String readxmlfile = "D:" + File.separator + "Readxmlfile.xml"; String writexmlfile = "D:" + File.separator + "Writexmlfile . xml "; String readtxtfile =" D: "+ File.separator +" Readtxtfile.txt "; &NBS P String writetxtfile = "D:" + File.separator + "Writetxtfile.txt"; Readpropertiesfile (rea Dfile); Read the properties file writepropertiesfile (WriteFile); Write properties file Readpropertiesfilefromxml (readxmlfile); Read XML file Writepropertiesfiletoxml (writexmlfile); Write XML file readpropertiesfile (readtxtfile); Read TXT file writepropertiesfile (writetxtfile);Write TXT file } //Read resource file and handle Chinese garbled public static void Readpropertiesfile (String FileName) { Properties Properties = New Properties (); T ry { InputStream InputStream = new FileInputStream (fil ename); properties.load (InputStream); in Putstream.close (); Close stream } catch (IOException e) { &NB Sp e.printstacktrace (); } String username = Properties.getproperty ("username"); String Passsword = properties.getproperty ("password"); String Chinese = properties.getproperty ("Chinese"); try   ; { Chinese = new String (chinese.getbytes ("iso-8859-1"), "GBK"),//handling Chinese garbled &N Bsp } catch (unsupportedencodingexception e) {  ; e.printstacktrace (); } System.out . println (username); SYSTEM.OUT.PRINTLN (Passsword); System.out.println (Chinese); } //read XML file and handle Chinese garbled public static void Readpropertiesfilefromxml (String filename) { Properties Properties = new Properti ES (); try { InputStream INP Utstream = new FileInputStream (filename); Properties.loadfromxml (InputStream); inputStream.Close (); } catch (IOException e) { e.printstacktrace (); } STR ing username = properties.getproperty ("username"); String Passsword = Properties.getproperty ( "Password"); String Chinese = properties.getproperty ("Chinese"); Chinese in XML is not garbled, normal display SYSTEM.OUT.PRINTLN (username); System.out.println (Passsword); SYSTEM.OUT.PRINTLN (Chinese); } //write resource file with Chinese public static void Writepropertiesfile (String filename) { &NBSP ; Properties Properties = new properties (); try { OutputStream outputstream = new FileOutputStream (filename); Properties.setproperty ("username", "myname"); Properties.setproperty ("Password", "MyPassword"); Properties.setproperty ("Chinese", "Chinese"); Properties.store (OutputStream, " Author: [email protected] "); outputstream.close (); & nbsp } catch (IOException e) { E . Printstacktrace (); } } //write resource file to XML file with Chinese public static void Writepropertiesfiletoxml (String filename) { Properties Properties = NE W Properties (); try { OUTPU TStream outputstream = new FileOutputStream (fileName); Properties.setproperty ("username", "myname"); Properties.setproperty ("Password", "MyPassword"); Properties.setproperty ("Chinese", "Chinese"); Properties.storetoxml (OutputStream, "Author: [email protected]"); outputstream.close (); & nbsp } catch (IOException e) { E . Printstacktrace (); } } } The resource files required to run this program, I was placed in the D packing directory, such as d:/ Readfile.properties1. readfile.propertiesusername=mynamepassword=mypasswordchinese= Chinese 2. Writefile.properties#author: [email protected] #Fri may 22:19:44 CST 2010password=mypasswordchinese=/u4e2d/ u6587username=myname 3. readxmlfile.xml <?xml verSion= "1.0" encoding= "UTF-8" standalone= "no"? ><! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" ><properties><entry key= "password" >mypassword</entry><entry key= "Chinese" > Chinese </entry><entry key= "username" >myname</ entry></properties> 4. Writexmlfile.xml<?xml version= "1.0" encoding= "UTF-8" standalone= "no"? ><! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" ><properties><comment>author: [ Email protected]</comment><entry key= "password" >mypassword</entry><entry key= "Chinese" > Chinese </entry><entry key= "username" >myname</entry></properties> 5. readtxtfile.txt username=myname password =mypassword chinese= Chinese 6. Writetxtfile.txt#author: [email protected] #Fri may 22:25:16 CST2010password=mypasswordchinese=/u4e2d/u6587username=myname
Java read-write properties file