
Java中配置文件的三种配置位置
一:配置文件存在位置有三种 ? ①存放于根目录下,/代表获取src根目录的绝对路径 ? ②存放于同一类的包下,不加/代表同类名包下的相对路径; ? ③存放于WEB-INF目录下
xml作用:
*.xml
*.properties
①同包
package com.hmf;?
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
/** ?* 1.同包?Text01.class.getResourceAsStream("db.properties"); ?* Properties文件是一种资源文件 ?* @author T440s ?* ?*/ public class Text01 { ?? ?public static void main(String[] args) throws Exception { ?? ??? ?InputStream ins = Text01.class.getResourceAsStream("db.properties"); ?? ??? ?Properties pt=new Properties(); ?? ??? ?pt.load(ins); ?? ??? ?System.out.println(pt.getProperty("uname")); ?? ??? ?System.out.println(pt.getProperty("upass")); ?? ?}
} ?
②?src目录下

?③WEB-INF
package com.hmf;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/text") public class TextServlet extends HttpServlet{ ?? ?protected void service(HttpServletRequest req,HttpServletResponse rep) throws ServletException ,IOException{ ?? ??? ?InputStream ins = req.getServletContext().getResourceAsStream("/WEB-INF/db.properties"); ?? ??? ?Properties p=new Properties(); ?? ??? ?p.load(ins); ?? ??? ?System.out.println(p.getProperty("uname")); ?? ??? ?System.out.println(p.getProperty("upass")); ?? ?}
} ?
下面是我报错的地方? 千万不要加双引号 加了就变成字符串了

二: 找s003的内容
思路 ?? ??? ? ?1.读取到students.xml的的内容 ?? ??? ? ?2.获取到所以students标签的内容 ?? ??? ? ?3.获取到students标签所以的sid ?? ??? ? ?4.获取sid=s003时候,获取name标签内容
package com.hmf;
import java.io.InputStream; import java.util.List;
import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader;
public class Text02 { ?? ??? ?/** ?? ??? ? * 思路 ?? ??? ? * 1.读取到students.xml的的内容 ?? ??? ? * 2.获取到所以students标签的内容 ?? ??? ? * 3.获取到students标签所以的sid ?? ??? ? * 4.获取sid=s003时候,获取name标签内容 ?? ??? ? *? ?? ??? ? */ ?? ?public static void main(String[] args) throws Exception { ?? ??? ?//获取文件的io流 ?? ??? ?InputStream ins = Text02.class.getResourceAsStream("/students.xml"); ?? ??? ?//创建xml文件的saxReader对象 ?? ??? ?SAXReader srd=new SAXReader(); ?? ??? ?Document doc= srd.read("ins"); ?? ??? ?//selectNodes:获取对应的对象 ?返回list ?? ??? ?List<Element> stu = doc.selectNodes("/students/student"); ?? ??? ?for (Element st : stu) { ?? ??? ?//attributeValue:获取指定的属性值 ?? ??? ?String sid=st.attributeValue("sid"); ?? ??? ? ?if("s003".equals(sid)) { ?? ??? ?//selectSingleNode:获取对应的对象 ? 返回单个属于元素的对象 ?? ??? ??? ? Element nameEl = (Element) st.selectSingleNode("name"); ?? ??? ??? ? System.out.println(nameEl.getText()); ?? ??? ??? ?} ?? ??? ?} ?? ?} } ?
三:例如
1:获取action中type的值
2:获取第二个action中type的值
3:获取第二个action所有forward的path
4:获取第二个action的第二个forward的path
package com.hmf;
import java.io.InputStream; import java.util.List; ? import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader;
public class config { ?? ?public static void main(String[] args) throws Exception{ ?? ??? ?//获取读取给文件的io流 ?? ??? ?InputStream ins = config.class.getResourceAsStream("/config.xml"); ?? ??? ?//创建读取xml文件的saxReader对象 ?? ??? ?SAXReader sa = new SAXReader(); ?? ??? ?//获取文件的document对象 ?? ??? ?Document doc = sa.read(ins); ?? ??? ?//找到config下的所有action节点 ?? ??? ?List<Node> node1 = doc.selectNodes("/config/action"); ?? ??? ?//遍历action节点 ?? ??? ?for (Node n : node1) { ?? ??? ?//将节点对象转换成为元素对象 ?? ??? ?Element em=(Element)n; ?? ??? ?//找到type属性 ?? ??? ?String value = em.attributeValue("type"); ?? ??? ?System.out.println(value); ?? ??? ?} ?? ??? ?System.out.println("===="); ?? ??? ?//config节点下path属性为/loginAction的action节点 ?? ??? ? Node two = doc.selectSingleNode("/config/action[@path='/loginAction']"); ?? ??? ? Element el=(Element)two; ?? ??? ? //获取该节点下的type属性 ?? ??? ? String type = el.attributeValue("type"); ?? ??? ? System.out.println(type); ? ? ? ? ?System.out.println("===="); ?? ??? ?//获取config节点下的forward节点 ?? ??? ?List<Node> forwardn = two.selectNodes("forward"); ?? ??? ?for (Node node : forwardn) { ?? ??? ??? ?Element els=(Element)node; ?? ??? ?//获取config节点下的forward节点的path属性 ?? ??? ??? ?String path = els.attributeValue("path"); ?? ??? ??? ?System.out.println(path); ?? ??? ?} ?? ??? ?System.out.println("===="); ?? ??? ?Node name = doc.selectSingleNode("/config/action[@path='/loginAction']/forward[@name='success']"); ?? ??? ?Element elx=(Element)name; ?? ??? ?String twoPath = elx.attributeValue("path"); ?? ??? ?System.out.println(twoPath);
?? ?}
} ?

?
?好啦 今天就到这里 希望可以帮助到你!!!
|