java 读取配置xml_java读配置文件(xml、property)的简单例子

读property文件的例子:

package com.test;

import java.io.InputStream;

import java.util.Properties;

/**

*

* CopyRight (C) www.blogjava.net/ilovezmh  All rights reserved.

*

* WuHan Inpoint Information Technology Development,Inc.

*

* Author zhu

*

* @version 1.0    2007-2-2

*

*

Base on : JDK1.5

*

*/

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内容如下:

//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.blogjava.net/ilovezmh All rights reserved.

*

* WuHan Inpoint Information Technology Development,Inc.

*

* Author zhu

*

* @version 1.0    2007-2-1

*

*

Base on : JDK1.5

*

*/

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文件如下:

程序员

10.00

青年文摘

3.00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值