一.讀取指定位置下的資源文件
src根目錄下
類名.class.getResourceAsStream("/文件名");
與讀取配置文件的類在同一包
類名.class.getResourceAsStream("文件名");
WEB-INF(或其子目錄下)
ServletContext servletContext=request.getServletContext();
InputStream ins = servletContext.getResourceAsStream("/WEB-INF/文件名");
二.解析指定路徑下的資源文件:
properties文件的解析方式有java.util.properties這個類來完成(properties文件以鍵值對的形式存在,需通過鍵名來獲取值)
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiseDemo { public static void main(String[] args) throws IOException { //獲取到同包下的資源文件 將其轉化為流對象 InputStream ins=PropertiseDemo.class.getResourceAsStream("/db.properties"); //需要專業的工具類來將流中的數據解析出來 Properties p=new Properties(); p.load(ins); System.out.println("uname"); System.out.println("upass"); } }
解析xml文件
xml文件傳統解析方式有dom4解析jdk/jdom解析,sax解析
jdk/jdom 和 sax解析方式都是 由上往下解析
dom4j解析是 由外到內解析,需要導包(dom4j-1.6.1.jar,)
由於jdk/jdom和sax解析解析步驟比較復雜,使用的人較少
xpath等同數據庫的select語句
document.selectNodes(xpath);//查一組
document.selectSingleNode(xpath);//查單個
import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Dome02 { public static void main(String[] args) throws Exception { // dom4j+path 解析xml文件 SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("D:\\eclipse\\j2ee06\\src\\com\\temp\\config.xml")); // 查一組 for (Object o : document.selectNodes("/config/action/forward")) { Element element = (Element) o; System.out.print(element.attributeValue("mane")); System.out.print(element.attributeValue("path")); System.out.println(element.attributeValue("redirect")); } //查詢一個 Element element=(Element)document.selectSingleNode("/config/action"); System.out.println(element.attributeValue("path")); } }
列如:
import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Temp { public static void main(String[] args) throws Exception { InputStream inputStream = Temp.class.getResourceAsStream("/config.xml"); SAXReader sax = new SAXReader(); Document document = sax.read(inputStream); // 1. 獲取所有action中type的值 List<Element> stuElement = document.selectNodes("/config/action"); for (Element element : stuElement) { String type = element.attributeValue("type"); System.out.println(type); } // 2.獲取第二個action中的type的值 for (Element element : stuElement) { if ("/loginAction".equals(element.attributeValue("path"))) { String type = element.attributeValue("type"); System.out.println(type); } } // 3.獲取第二個action的所有forward的path for (Element element : stuElement) { if ("/loginAction".equals(element.attributeValue("path"))) { List<Element> forward = element.selectNodes("forward"); for (Element forwardElement : forward) { String path = forwardElement.attributeValue("path"); System.out.println(path); } } } // 4、獲取第二個action的第二個forward的path for (Element element : stuElement) { if ("/loginAction".equals(element.attributeValue("path"))) { List<Element> forward = (List<Element>) element.selectNodes("forward"); for (Element forwardElement : forward) { if ("success".equals(forwardElement.attributeValue("name"))) { String path = forwardElement.attributeValue("path"); System.out.println(path); } } } } } }