读取/书写Java的XML格式properties文件
生活随笔
收集整理的這篇文章主要介紹了
读取/书写Java的XML格式properties文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載自??讀取/書寫Java的XML格式properties文件
在JDK5中,properties文件的格式可以由XML構(gòu)成,這里給出了一個(gè)讀取/書寫XML格式properties文件的例子。
因?yàn)槭褂昧薠ML,所以文件內(nèi)容支持了CJKV(中文、日文、韓文、越南語(yǔ))。可以直接書寫、調(diào)用。
?
例子代碼:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; /*** * * @author 郝春利* @since 2008/09/11* @version $Revision:$*/ public class PropertiesTest {/*** @param args*/public static void main(String[] args) {// 書寫properties文件Properties properties = new Properties();properties.put("ONE", "1");properties.put("TWO", "2");properties.put("中文", "看看中文怎么樣");properties.put("日本語(yǔ)", "日本語(yǔ)はどう?");properties.put("???", "???");properties.put("Th?o lu?n ti?ng Vi?t", "Th?o lu?n ti?ng Vi?t");OutputStream stream = null;http://www.loveapple.cntry {stream = new FileOutputStream("temp.xml");properties.storeToXML(stream, "Temporary Properties");}catch (IOException ex) {ex.printStackTrace();}finally{try{stream.close();}catch(Exception e){}}// 讀取properties文件Properties properties2 = new Properties();InputStream is = null ;try{is = new FileInputStream("temp.xml");properties2.loadFromXML(is);System.out.println(properties2);}catch (IOException e) {e.printStackTrace();}finally{try{is.close();}catch (Exception e) {}}} }輸出結(jié)果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Temporary Properties</comment> <entry key="TWO">2</entry> <entry key="ONE">1</entry> <entry key="???">???</entry> <entry key="Th?o lu?n ti?ng Vi?t">Th?o lu?n ti?ng Vi?t</entry> <entry key="日本語(yǔ)">日本語(yǔ)はどう?</entry> <entry key="中文">看看中文怎么樣</entry> </properties>?
總結(jié)
以上是生活随笔為你收集整理的读取/书写Java的XML格式properties文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑最高配置是什么配置(电脑的最高配置)
- 下一篇: mybatis入门(二)之XML 映射配