1. Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)
1.2 存放位置
1.2.2 与读取配置文件的类在同一包
Xxx.class.getResourceAsStream(“config2.properties”);
代码:
package pengyuxuan;
/**
* 读取指定位置下的资源文件(db.properties)
* 1.读取同包下的资源文件
* 2.资源文件存放在跟目录
* 3.资源文件存放在web-ing下
*
* 何为sourec folder
* 就是代码不将其当做文件来处理,程序员用来做文件归类所用
*
* 程序运行时class文件
*/
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"));
}
}
运行结果
1.2.1 src根目录下
Xxx.class.getResourceAsStream("/config.properties");
代码,和结果
1.2.3 WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream("/WEB-INF/config3.properties");
package pengyuxuan;
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 {
private static final long serialVersionUID = 8791135087123533262L;
@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 {
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"));
}
}
2.解析xml文件
xml文件为:
2.1方法一
代码:
package pengyuxuan;
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 XmIDemo {
public static void main(String[] args) throws Exception{
InputStream in=XmIDemo.class.getResourceAsStream("students.xml");
SAXReader saxReader=new SAXReader();
Document doc=saxReader.read(in);
System.out.println(doc.asXML());
// xpath解析能够将xml格式的串当做目录的结构来查找
List<Element> selectNodes=doc.selectNodes("/students/student");
for (Element stuEles : selectNodes) {
if ("s002".equals(stuEles.attributeValue("sid"))) {
System.out.println(stuEles.asXML());
Element nameEle=(Element)stuEles.selectSingleNode("name");
System.out.println(nameEle.asXML());
System.out.println(nameEle.getText());
}
}
}
结果:
2.2方法二
代码,结果
3.作业
package pengyuxuan;
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 XmIDemo {
public static void main(String[] args) throws Exception{
InputStream in=XmIDemo.class.getResourceAsStream("config.xml");
SAXReader saxReader=new SAXReader();
Document doc=saxReader.read(in);
// 作业:config.xml解析
// 1、获取所有action中的type的值
List<Element> selectNodes1=doc.selectNodes("/config/action");
for (Element object1 : selectNodes1) {
String type = object1.attributeValue("type");
System.out.println(type);
}
// 2、获取第二个action中的type的值
List<Element> selectNodes2=doc.selectNodes("/config/action[@path=\'/loginAction\']");
for (Element object2 : selectNodes2) {
String type = object2.attributeValue("type");
System.out.println(type);
}
// 3、获取第二个action的所有forward的path
List<Element> selectNodes3=doc.selectNodes("/config/action[@path=\'/loginAction\']/forward");
for (Element object3 : selectNodes3) {
String type = object3.attributeValue("path");
System.out.println(type);
}
// 4、获取第二个action的第二个forward的path
List<Element> selectNodes4=doc.selectNodes("/config/action[@path=\'/loginAction\']/forward[@name=\'success\']");
for (Element object4 : selectNodes4) {
String type = object4.attributeValue("path");
System.out.println(type);
}
}
}
结果: