国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java讀配置文件(xml、property)的簡單例子

讀property文件的例子:

package com.test;

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

/**
 * 
 * CopyRight (C) www.blogjava.net/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都是可以的,只是使用的時(shí)候路徑需要注意下

    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.blogjava.net/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>

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
maven工程中讀取resource目錄下配置文件
EHCache - 單落撒旦的日志 - 網(wǎng)易博客
EHCache 單獨(dú)使用
使用JDOM生成XML文檔
".properties" 配置文件的3種使用方式(源碼) 1.0 -- 可用 JUnit 檢測
讀取properties文件的6種方式,建議收藏!
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服