读取指定位置下的资源文件(db.properties)
1.读取同包下的资源文件
文件:db.properties
读取同包下的资源文件代码块
package com.chenkang;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取指定位置下的资源文件(db.properties)
* 1.读取同包下的资源文件
* @author 20190528
*
*/
public class propertiesDemo {
public static void main(String[] args) throws IOException {
// 获取到同包下的资源文件,将其转换成流对象
InputStream in = propertiesDemo.class.getResourceAsStream("db.properties");
// 需要专门的工具类来讲流中的数据解析出来
Properties p=new Properties();
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));
}
}
输出结果
2.读取同包下的资源文件
比如: Xxx.class.getResourceAsStream("/config.properties");
运行结果和代码和上面的一样 运行结果也是一样 只需要在文件路径加上一个斜杠
3…读取资源文件放在web-inf的资源文件 ()
首先建一个Servlet
package com.chenkang;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ParseServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = req.getServletContext();
InputStream in = context.getResourceAsStream("/WEB-INF/db.properties");
Properties p=new Properties();
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));
}
}
然后配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>xml</display-name>
<servlet>
<servlet-name>parseServlet</servlet-name>
<servlet-class>com.chenkang.ParseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>parseServlet</servlet-name>
<url-pattern>/parseServlet</url-pattern>
</servlet-mapping>
</web-app>
然后Tomcat运行
运行结果
解析指定路径下的资源文件(student.xml)
1.需要用到的架包
2.代码块
package com.chenkang;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
/**
* 解析指定路径下的资源文件
* @author 20190528
*
*/
public class XmlDemo {
public static void main(String[] args) throws Exception {
InputStream in = XmlDemo.class.getResourceAsStream("students.xml");
SAXReader saxReader=new SAXReader();
Document doc = saxReader.read(in);
System.out.println(doc.asXML());
}
}
3.运行结果
xpath解析
xpath解析能够将xml格式的串当作目录结构来进行查找
在上面代码上加上以下代码
List<Element> stuEles = doc.selectNodes("/students/student");
// for (Element stuEle : stuEles) {
// if("s002".equals(stuEle.attributeValue("sid"))) {
System.out.println(stuEle.asXML());
// Element name = (Element) stuEle.selectSingleNode("name");
// System.out.println(name.asXML());
// System.out.println(name.getText());
// }
// }
输出结果
1、获取所有action中的type的值
2、获取第二个action中的type的值
3、获取第二个action的所有forward的path
4、获取第二个action的第二个forward的path
代码:
package com.chenkang;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class aa {
public static void main(String[] args) throws Exception {
InputStream in = XmlDemo.class.getResourceAsStream("config.xml");
// dom4j包下的SAXReader
SAXReader saxReader=new SAXReader();
// 读取流中的数据 saxReader.read(in)返回一个Document(dom4j包下)
Document doc = saxReader.read(in);
// 1.获取所有action中的type的值
List<Element> stuElems = doc.selectNodes("/config/action");
for (Element stuEle : stuElems) {
String type = stuEle.attributeValue("type");
System.out.println(type);
}
// 2.获取第二个action中的type的值
System.out.println("---------------------------");
List<Node> list = doc.selectNodes("/config/action[@path='/loginAction']");
for (Node node : list) {
Element elem = (Element) node;
String type = elem.attributeValue("type");
System.out.println(type);
}
// 3.获取第二个action的所有forward的path
System.out.println("---------------------------");
List<Node> list2 = doc.selectNodes("/config/action[@path='/loginAction']/forward");
for (Node node : list2) {
Element elem = (Element) node;
String path = elem.attributeValue("path");
System.out.println(path);
}
// 4.获取第二个action的第二个forward的path
System.out.println("---------------------------");
List<Node> list3 = doc.selectNodes("/config/action[@path='/loginAction']/forward[@name='success']");
for (Node node : list3) {
Element elem = (Element) node;
String path = elem.attributeValue("path");
System.out.println(path);
}
}
}
输出结果: