<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    乖,別哭的薄殼
    ~一份耕耘,一份收獲~
    posts - 23,comments - 260,trackbacks - 0

    讀property文件的例子:

    package com.test;

    import java.io.InputStream;
    import java.util.Properties;

    /**
     *
     * CopyRight (C) www.tkk7.com/ilovezmh  All rights reserved.<p>
     *
     * WuHan Inpoint Information Technology Development,Inc.<p>
     *
     * Author zhu<p>
     *
     * @version 1.0    2007-2-2
     *
     * <p>Base on : JDK1.5<p>
     *
     */

    public class ReadPro {
     
     public String getPara(String fileName) {
      
      Properties prop= new Properties();
      try {
       //ClassLoader cl = this.getClass().getClassLoader(); 
       //InputStream is = cl.getResourceAsStream(fileName);
       InputStream is = this.getClass().getResourceAsStream(fileName);
       prop.load(is);
       if(is!=null) is.close();
      }
      catch(Exception e) {
       System.out.println(e+"file "+fileName+" not found");
      }
      return prop.getProperty("ip");
     }
     
     public static void main(String[] args) {
      ReadPro pro = new ReadPro();
      System.out.println(pro.getPara("ip.property"));
     }
     
     //-----------------------------------
     //ip.property內(nèi)容如下:
     //ip:192.168.0.1
     
    }


    注意:上面使用Class和ClassLoader都是可以的,只是使用的時候路徑需要注意下

        Class是把class文件所在的目錄做為根目錄,

        ClassLoader是把加載所有classpath的目錄為根目錄,也就是“..../classes”。

        如:

        使用ClassLoader:this.getClass().getClassLoader().getResourceAsStream("com/test/ip.property");

        使用Class:this.getClass().getResourceAsStream("/com/test/ip.property");

                            如果類與配置文件在同一目錄下,也可

                            this.getClass().getResourceAsStream("ip.property");


    用jdom讀xml文件的例子:
    (jdom下載)

    package com.test;
    import java.io.*;
    import java.util.*;

    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.XMLOutputter;
    import org.jdom.output.Format;

    /**
     *
     * CopyRight (C) www.tkk7.com/ilovezmh All rights reserved.<p>
     *
     * WuHan Inpoint Information Technology Development,Inc.<p>
     *
     * Author zhu<p>
     *
     * @version 1.0    2007-2-1
     *
     * <p>Base on : JDK1.5<p>
     *
     */
     public class XMLReader {
     
      public void createXML(){
      
       Element root=new Element("books");
      
       Document doc=new Document(root);
       Element book1 = new Element("book");
       //Element name1=new Element("name");
       //name1.setAttribute(new Attribute("hot","true"));
       //name1.addContent("程序員");
       //Element price1=new Element("price");
       //price1.addContent("10.00");
       //book1.addContent(name1);
       //book1.addContent(price1); 
      
       Element book2 = new Element("book");
       //Element name2=new Element("name");
       //name2.setAttribute(new Attribute("hot","false"));
       //name2.addContent("讀者");
       //Element price2=new Element("price");
       //price2.addContent("3.00");
       //book2.addContent(name2);
       //book2.addContent(price2);
         
       book1.addContent(new Element("name").addContent("程序員").setAttribute("hot","true"));
       book1.addContent(new Element("price").addContent("10.00"));
       book2.addContent(new Element("name").addContent("青年文摘").setAttribute("hot","false"));
       book2.addContent(new Element("price").addContent("3.00"));
       root.addContent(book1);
       root.addContent(book2);
      
       try
       {
        XMLOutputter outer=new XMLOutputter(Format.getPrettyFormat().setEncoding("gb2312"));
        outer.output(doc,new FileOutputStream("book.xml"));
       }catch(IOException e){
        e.printStackTrace();
       }
      }
     
      public void readXML() throws FileNotFoundException, IOException{
      
       Document myDoc=null;
       try
       {
        SAXBuilder sb=new SAXBuilder();
        myDoc=sb.build(new FileInputStream("book.xml"));
       }catch(JDOMException e){
        e.printStackTrace();
       }catch(NullPointerException e){
        e.printStackTrace();
       }
      
       Element root=myDoc.getRootElement(); //先得到根元素
       List books=root.getChildren();//root.getChildren("book"); 
       for (Iterator iter1 = books.iterator();iter1.hasNext(); ) {
           Element book = (Element) iter1.next();
           System.out.println("bookname:"+book.getChild("name").getText());
           System.out.println("hot:"+book.getChild("name").getAttributeValue("hot"));
           System.out.println("price:"+book.getChild("price").getText());
       }
      
      }
      
      public static void main(String args[]) throws FileNotFoundException, IOException {
      
       XMLReader x=new XMLReader();
       x.createXML();
       x.readXML();

     }

    }

    生成的book.xml文件如下:
    <?xml version="1.0" encoding="gb2312"?>
    <books>
      <book>
        <name hot="true">程序員</name>
        <price>10.00</price>
      </book>
      <book>
        <name hot="false">青年文摘</name>
        <price>3.00</price>
      </book>
    </books>

    posted on 2007-02-01 17:17 小祝 閱讀(11184) 評論(17)  編輯  收藏 所屬分類: java技術(shù)

    FeedBack:
    # re: 一個用jdom讀寫xml文件的簡單例子
    2007-02-01 22:42 | 施偉
    呵呵,8錯8錯,我正要用這呢
    持續(xù)關(guān)注  回復(fù)  更多評論
      
    # re: 一個用jdom讀寫xml文件的簡單例子
    2007-02-02 11:24 | 梅穎
    5555....  回復(fù)  更多評論
      
    # re: 一個用jdom讀寫xml文件的簡單例子
    2007-02-02 13:04 | 小祝
    哭什么呀?誰欺負你了?  回復(fù)  更多評論
      
    # re: 一個用jdom讀寫xml文件的簡單例子
    2007-02-02 14:26 | 梅穎
    他灑,搶我的位置。。  回復(fù)  更多評論
      
    # re: 一個用jdom讀寫xml文件的簡單例子
    2007-02-02 14:30 | 小祝
    呵呵  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-05 19:43 | 睿不可當(dāng)
    呵呵,mark!  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-05 22:24 | 施偉
    繼續(xù)加油啊,更新啊更新啊 呵呵  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-06 09:29 | 梅穎
    發(fā)表一點感言好不?  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-06 11:02 | sinoly
    @梅穎
    @施偉
    你們真是blogjava應(yīng)該聘請的人才。。。絕對可以支撐一片天空滴說  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-06 11:44 | 小祝
    9494
    你們太強了。。。  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-07 10:46 | 梅穎
    又怎么了啊?呵呵,有人招我們?  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-08 15:27 | kk
    第一個例子根本沒用  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-08 16:21 | 睿不可當(dāng)
    第一個例子根本沒用?不是吧!  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-08 17:33 | 梅穎
    人氣漸漲啊  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2007-02-08 18:45 | 小祝
    謝謝大家支持啊~呵呵。
    我這里寫的都是些簡單的例子,只是說有時候會很有用的,擴展的話還是靠自己了。算是留個印像在這里了,用的時候也不致于忘記,呵呵。
    thank you !
      回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2009-06-02 13:23 | Dimmacro
    很好啊,每次不想學(xué)習(xí)的時候都來看看,發(fā)現(xiàn)你們都在進步中,我就重新燃起學(xué)習(xí)的熱情。。。。  回復(fù)  更多評論
      
    # re: java讀配置文件(xml、property)的簡單例子
    2012-05-31 16:36 | 一江
    正在學(xué)習(xí)如何讀取java 配置文件的方法,學(xué)習(xí)了  回復(fù)  更多評論
      
    主站蜘蛛池模板: 免费看大黄高清网站视频在线| 一级毛片在线免费看| 好吊妞在线成人免费| 国产亚洲玖玖玖在线观看| 成人免费毛片内射美女APP | 亚洲精品天堂成人片?V在线播放| 亚洲日本VA午夜在线影院| 四虎www免费人成| 国产亚洲精品美女久久久久| 国产一区二区免费在线| 免费精品国产自产拍在线观看| 亚洲国产精品13p| 中文字幕久无码免费久久| 亚洲色成人网站WWW永久| 最好免费观看高清在线| 亚洲图片一区二区| 国产精品久久久久久久久免费 | 亚洲人成网站在线观看播放动漫| 亚洲精品视频免费在线观看| 亚洲一区二区三区深夜天堂| 成年美女黄网站色大免费视频| 毛片亚洲AV无码精品国产午夜| 亚洲色一色噜一噜噜噜| A级毛片高清免费视频在线播放| 亚洲综合亚洲国产尤物| 成人男女网18免费视频| 一级特黄色毛片免费看| 亚洲av鲁丝一区二区三区| 毛片免费vip会员在线看| 一级a性色生活片久久无少妇一级婬片免费放 | 18勿入网站免费永久| 亚洲AV成人无码网站| 国产亚洲日韩一区二区三区| 51精品视频免费国产专区| 亚洲av午夜电影在线观看 | 亚洲成年人在线观看| 免费av欧美国产在钱| 尤物视频在线免费观看| 亚洲视频免费观看| 在线看片无码永久免费aⅴ| 色www永久免费|