读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>
分享到:
相关推荐
Ini文件是一种常见的配置文件格式,通常用于存储程序的设置和选项。在Windows系统中,许多应用程序使用 Ini 文件来保存用户配置。在Java中,虽然没有内置的Ini文件处理库,但我们可以借助第三方库如JIniFile或者...
首先,XML(eXtensible Markup Language)是一种用于标记数据的语言,广泛用于数据交换和配置文件。它的结构清晰,易于人类阅读和机器解析。Dom4j是一个流行的Java XML API,它提供了灵活而强大的XML处理功能。通过...
标题中的“解析XML和Propertites配置文件”指的是在软件开发中处理两种常见的配置文件格式。XML(eXtensible Markup Language)是一种用于存储和传输数据的标记语言,具有良好的结构化特性,常用于应用程序的配置、...
在Java中,我们可以使用内置的`java.util.Properties`类来处理.properties文件,而对于.xml配置文件,则可以使用DOM、SAX或JAXB解析器进行处理。 以下是一个使用`Properties`类读取.properties配置文件的基本步骤:...
`.txt`文件是最常见的文本文件,`.properties`文件用于存储配置信息,而`.xml`文件则是一种结构化的数据存储格式,常用于数据交换和配置。 首先,我们来看`.txt`文件的读写。在Java中,我们可以使用`java.io`包中的...
本文将深入探讨如何在Java中读取和操作`properties`配置文件。 1. **什么是Properties文件** `properties`文件是一种文本文件,其内容遵循特定的语法,主要用于存储程序的配置信息,如数据库连接字符串、应用设置...
根据给定文件的信息,我们可以总结出...总结以上内容,我们了解了如何在 Java 中使用 `Properties` 类进行配置文件的读写操作,以及如何使用 DOM 解析器来处理 XML 文件。这些技术是开发过程中经常用到的基本技能之一。
XML(Extensible Markup Language)和属性文件(Properties File)是两种常见的数据存储格式,它们在软件开发中扮演着重要角色...同时,合理地组织和管理配置文件(如conf目录中的文件),能有效提升项目的结构清晰度。
在Mycat配置文件(mycat-conf.xml)中,定义数据节点(datanode)和数据源(datasource)。每个数据源对应一个MySQL实例,可以设置为主或者从。例如: ```xml <dbhost>192.168.1.1 <dbport>3306 <dbname>test ...
本文将深入探讨如何使用Java与XML进行交互,特别是在连接数据库和将数据库内容导出为XML格式文件的场景下。 首先,我们需要了解Java中的XML处理技术。主要有DOM(Document Object Model)、SAX(Simple API for XML...
了解了这些配置文件后,你可以根据业务需求调整参数,实现数据库的负载均衡、读写分离、故障切换等功能。Mycat 还支持 SQL 扩展,允许自定义 SQL 语句处理,增强其功能。 在实际操作中,你需要通过命令行或图形界面...
本篇将详细讲解基于给定的`ReadAndPrintXMLFile.java`、`MyProperties.java`和`book.xml`文件的Java文件读写知识。 首先,`MyProperties.java`可能是一个用于处理`.properties`文件的类。在Java中,`java.util....
`property-utils-1.9`可能指的是Apache Commons Lang库的一部分,它提供了对Java属性文件的读写和操作功能。在部署和配置Web应用时,这些工具可以帮助管理配置文件,比如设置CORS策略。 6. **配置CORS Filter**: ...
由于XML具有良好的可读性和结构化,它被广泛地应用于配置文件、数据交换等方面。在Java中,操作XML的常用方法有四种,它们分别是DOM(Document Object Model)、SAX(Simple API for XML)、StAX(Streaming API for...
2. **修改 Amoeba 服务器上的配置文件** (`/usr/local/amoeba/conf/dbServers.xml`): - 在配置文件中,可以看到关于数据库连接池以及虚拟服务器的相关配置。 - 首先,定义了一个抽象的数据库服务器配置 `...
这可以通过Java代码或者配置文件(如XML)来完成。以XML配置为例,可以创建一个`db-config.xml`文件,为每个数据库定义一个数据源: ```xml <property name="driverClassName" value="com.mysql.jdbc.Driver"/> ...
总的来说,CORS-filter-1.7.jar和java-property-utils-1.9.jar这两个库在Java Web开发中扮演着重要角色,一个是解决前后端跨域问题,另一个则是处理配置文件的读写。理解并掌握这两个库的使用,对于提升Web应用的...
总结,`org.apache.commons.configuration.XMLConfiguration` 是一个强大的工具,能够方便地处理XML配置文件,提供灵活的读写和监听机制。通过熟练掌握它的使用,你可以有效地管理你的应用程序配置,提高代码的可...
CORS Filter确保了前端和后端之间的通信不受同源策略限制,而Java Property Utils则帮助开发者有效地管理和维护应用的配置,提供了更好的可扩展性和灵活性。 在部署和使用这两个库时,开发者需要注意以下几点: 1....
总结,Java中XML的读写操作涵盖了DOM、SAX和JAXB等多种方式,每种方式都有其适用场景和优缺点。DOM适合小型XML文件,方便遍历和操作;SAX适用于大文件,资源消耗少;JAXB则提供了对象和XML间的无缝转换,适用于复杂...