一、Java中配置文件的三种配置位置及读取方式
1.1 两种配置文件:
XML和.properties(属性文件)*
1.2 存放位置(以db.properties为例)
1、同包下
读取方式代码如下:
package com.zhuyuncong.xml;
import java.io.IOException;
/**
* 读取同包下的资源文件
*/
import java.io.InputStream;
import java.util.Properties;
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、src根目录下
读取方式代码如下:
package com.zhuyuncong.xml;
import java.io.IOException;
/**
* 读取src根目录下的资源文件
*/
import java.io.InputStream;
import java.util.Properties;
public class PropertiesDemo2 {
public static void main(String[] args) throws IOException {
InputStream in = PropertiesDemo2.class.getResourceAsStream("/db.properties");
Properties p=new Properties();
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("url"));
}
}
结果如下:
3、WEB-INF(或其子目录下)
读取方式代码如下:
package com.zhuyuncong.xml;
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 doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(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("driver_Class"));
}
}
二、Xml解析
1、需要导入dom4j+xpath两个包
例:解析以下xml文件
代码如下:
package com.zhuyuncong.xml;
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;
/**
* 解析制定路径下的资源文件
* @author MACHENIKE
*
*/
public class XmlDemo {
public static void main(String[] args) throws Exception {
InputStream in = XmlDemo.class.getResourceAsStream("/students.xml");
SAXReader sax = new SAXReader();
Document doc = sax.read(in);
// System.out.println(doc.asXML());
// 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 nameEle = (Element)stuEle.selectSingleNode("name");
// System.out.println(nameEle.getText());
//
// }
// }
//方法二:
Element stu002Eles = (Element) doc.selectSingleNode("/students/student[@sid='s002']/name");
System.out.println(stu002Eles.getText());
}
}
以上俩个方法输出的都是:小芳
三、 作业:config.xml解析
config:
1、获取所有action中的type的值
2、获取第二个action中的type的值
3、获取第二个action的所有forward的path
4、获取第二个action的第二个forward的path
代码如下:
package com.zhuyuncong.xml;
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;
/**
* 1、获取所有action中的type的值
* 2、获取第二个action中的type的值
* 3、获取第二个action的所有forward的path
* 4、获取第二个action的第二个forward的path
* @author MACHENIKE
*
*/
public class ConfigDemo {
public static void main(String[] args) throws DocumentException {
InputStream in = ConfigDemo.class.getResourceAsStream("/config.xml");
SAXReader sax = new SAXReader();
Document doc = sax.read(in);
System.out.println("1、获取所有action中的type的值:");
List<Element> conEles = doc.selectNodes("/config/action");
for (Element conEle : conEles) {
String type = conEle.attributeValue("type");
System.out.println(type);
}
System.out.println("2、获取第二个action中的type的值:");
List<Element> conEles2 = doc.selectNodes("/config/action[@path='/loginAction']");
for (Element conEle : conEles2) {
String type = conEle.attributeValue("type");
System.out.println(type);
}
System.out.println("3、获取第二个action的所有forward的path:");
List<Element> conEles3 = doc.selectNodes("/config/action[@path='/loginAction']/forward");
for (Element conEle : conEles3) {
String path = conEle.attributeValue("path");
System.out.println(path);
}
System.out.println("4、获取第二个action的第二个forward的path:");
List<Element> conEles4 = doc.selectNodes("/config/action[@path='/loginAction']/forward[@name='b']");
for (Element conEle : conEles4) {
String path = conEle.attributeValue("path");
System.out.println(path);
}
}
}
结果: