乖,别哭的薄壳
~一份耕耘,一份收获~
posts - 23,comments - 260,trackbacks - 0

读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内容如下:
 //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.<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 小祝 阅读(11155) 评论(17)  编辑  收藏 所属分类: java技术

FeedBack:
# re: 一个用jdom读写xml文件的简单例子
2007-02-01 22:42 | 施伟
呵呵,8错8错,我正要用这呢
持续关注  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 11:24 | 梅颖
5555....  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 13:04 | 小祝
哭什么呀?谁欺负你了?  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 14:26 | 梅颖
他洒,抢我的位置。。  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 14:30 | 小祝
呵呵  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-05 19:43 | 睿不可当
呵呵,mark!  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-05 22:24 | 施伟
继续加油啊,更新啊更新啊 呵呵  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 09:29 | 梅颖
发表一点感言好不?  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 11:02 | sinoly
@梅颖
@施伟
你们真是blogjava应该聘请的人才。。。绝对可以支撑一片天空滴说  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 11:44 | 小祝
9494
你们太强了。。。  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-07 10:46 | 梅颖
又怎么了啊?呵呵,有人招我们?  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 15:27 | kk
第一个例子根本没用  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 16:21 | 睿不可当
第一个例子根本没用?不是吧!  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 17:33 | 梅颖
人气渐涨啊  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:45 | 小祝
谢谢大家支持啊~呵呵。
我这里写的都是些简单的例子,只是说有时候会很有用的,扩展的话还是靠自己了。算是留个印像在这里了,用的时候也不致于忘记,呵呵。
thank you !
  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2009-06-02 13:23 | Dimmacro
很好啊,每次不想学习的时候都来看看,发现你们都在进步中,我就重新燃起学习的热情。。。。  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2012-05-31 16:36 | 一江
正在学习如何读取java 配置文件的方法,学习了  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: