java配置文件路径_Java中配置文件的三种配置位置及读取方式

XML 和properties

properties:

1、存放于src根目录下

//获取到同包下的资源文件,将其转换成流对象

//InputStream is=PropertiesDemo.class.getResourceAsStream("/db.properties");

//需要专门的工具类来讲流中的数据

Properties p=new Properties();

p.load(is);

System.out.println(p.getProperty("uname"));

System.out.println(p.getProperty("upass"));

2、与读取配置文件的类在同一包

//InputStream is= PropertiesDemo.class.getResourceAsStream("db.properties");

Properties p=new Properties();

p.load(is);

System.out.println(p.getProperty("uname"));

System.out.println(p.getProperty("upass"));

3、存放在WEB-INF(或其子目录下)

//新建一个servlet类

ServletContext context=request.getServletContext();

InputStream is=context.getResourceAsStream("/WEB-INF/db.properties");

Properties p=new Properties();

p.load(is);

System.out.println(p.getProperty("uname"));

System.out.println(p.getProperty("upass"));

4. dom4j+xpath解析xml文件

InputStream is = XMLDemo.class.getResourceAsStream("students.xml");

//SAXReader saxReader = new SAXReader();

//Document doc = saxReader.read(is);

//System.out.println(doc.asXML());

xpath

//List stueles=doc.selectNodes("/students/student");

//for (Element element : stueles) {

if ("s002".equals(element.attributeValue("sid"))) {

System.out.println(element.asXML());

//Element name=(Element)element.selectSingleNode("name");

//System.out.println(name.asXML());

//System.out.println(name.getText());

}

//}

//Element stuele =(Element) doc.selectSingleNode("/students/student[@sid='s001']");

//System.out.println(stuele.asXML());

package com.lingerqi.demo;

import java.io.InputStream;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

/**

* 解析指定路径下的资源文件(dom4j)

*

* @author xyls

*

*/

public class XMLDemo {

public static void main(String[] args) throws Exception {

InputStream is = XMlDemo.class.getResourceAsStream("config.xml");

SAXReader reader = new SAXReader();

Document doc = reader.read(is);

List stueles = doc.selectNodes("/config/action");

for (Element element : stueles) {

Element forward = (Element) element.selectSingleNode("forward");

String type = element.attributeValue("type");

String path = element.attributeValue("path");

// if (path.equals("/loginAction")) {

// 2、获取第二个action中的type的值

// System.out.println(type);

// }

// 1、获取所有action中的type的值

// System.out.println(type);

// String fpath=forward.attributeValue("path");

// System.out.println(type);

if (element.attributeValue("path").equals("/loginAction")) {

List ford = (List) element.selectNodes("forward");

for (Element element2 : ford) {

String fpath = forward.attributeValue("path");

// 3、获取第二个action的所有forward的path

System.out.println(fpath);

}

}

if (path.equals("/loginAction")) {

// 4、获取第二个action的第二个forward的path

// System.out.println(fpath);

}

}

// if (path.equals("/loginAction")) {

// 4、获取第二个action的第二个forward的path

// System.out.println(fpath);

// }

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值