国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用Properties從文件獲取屬性

2019-11-18 11:22:00
字體:
來源:轉載
供稿:網友

import java.util.*;
import java.io.*;

/**
 * refer to http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index_eng.sHtml  </br>
 * or http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index.shtml
 */
public class LoadPRoperties
{
 public static void main(String[] args) throws Exception {
  Properties prop = new Properties();

  //load properties from configuration file
  System.out.println("From properties file:");
  FileInputStream fis = new FileInputStream("sample.properties");
  prop.load(fis);
     prop.list(System.out);
  System.out.println("/nThe foo property: " + prop.getProperty("foo"));
  
  //load properties from xml property file(Tiger new method)
  System.out.println("From xml file:");
  fis =new FileInputStream("sampleprops.xml");
  /**
   *<pre>The XML document must have the following DOCTYPE declaration:
   *<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   *the dtd file :
   *<?xml version="1.0" encoding="UTF-8"?>
   *<!-- DTD for properties -->
   *<!ELEMENT properties ( comment?, entry* ) >
   *<!ATTLIST properties version CDATA #FIXED "1.0">
   *<!ELEMENT comment (#PCDATA) >
   *<!ELEMENT entry (#PCDATA) >
   *<!ATTLIST entry key CDATA #REQUIRED>
         *</pre>
   */
  prop.loadFromXML(fis);  
  prop.list(System.out);
  System.out.println("/nThe foo property: " + prop.getProperty("foo"));
  
  //生成xml文件
  System.out.println("prodUCe a xml file");
  prop = new Properties();
  prop.setProperty("one-two", "buckle my shoe");
  prop.setProperty("three-four", "shut the door");
     prop.setProperty("five-six", "pick up sticks");
  prop.setProperty("seven-eight", "lay them straight");
     prop.setProperty("nine-ten", "a big, fat hen");
  FileOutputStream fos = new FileOutputStream("rhyme.xml");
  /*
   *default encoding is UTF-8,
   * if you need specify encoding,
   * use storeToXML(OutputStream os,String comment,String encoding) instead
   */
  prop.storeToXML(fos, "Rhyme");//prop.storeToXML(fos, "Rhyme","GBK");
     fos.close();//The specified stream remains open after storeToXML() returns,so must close obviously
  /**
   *生成的xml如下(DTD如上所述):
   *<?xml version="1.0" encoding="UTF-8"?>
   *<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   *<properties>
   *<comment>Rhyme</comment>
   *<entry key="seven-eight">lay them straight</entry>
   *<entry key="five-six">pick up sticks</entry>
   *<entry key="nine-ten">a big, fat hen</entry>
   *<entry key="three-four">shut the door</entry>
   *<entry key="one-two">buckle my shoe</entry>
   *</properties>
   */
 }
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福清市| 繁峙县| 田林县| 蒙自县| 靖宇县| 东台市| 武夷山市| 台山市| 宝清县| 汉沽区| 平远县| 崇礼县| 黄平县| 上蔡县| 乃东县| 盘锦市| 泸州市| 通渭县| 开原市| 德兴市| 新密市| 英德市| 凤凰县| 湘潭县| 汕头市| 邳州市| 锡林浩特市| 青川县| 金华市| 望都县| 韩城市| 南靖县| 志丹县| 繁峙县| 天门市| 麻江县| 库伦旗| 那曲县| 涪陵区| 新巴尔虎左旗| 思茅市|