Java中配置文件的三种配置位置及读取方式 1.同包下的资源文件读取 2.将所有的资源文件放在根目录下编译后所有的资源文件都会自动进web-inf下 3 WEB-INF(或其子目录下)
第一种方式:在同包的情况下获取 public class properitesDemo { public static void main(String[] args) throws IOException { InputStream in = properitesDemo.class.getResourceAsStream("db.properties");//获取到该包下的properites文件 Properties p = new Properties();//实列化properties p.load(in); //读取文件里的资源 String name = p.getProperty("uname"); //根据名字获取字符 System.out.println(name); } } 第二种方式:如果不是同包的情况下,而是放在根目录下,我们就只要 在 db.properties 加个斜杠 " / " 就好了 将 properitesDemo.class.getResourceAsStream("db.prope rties"); 改为 properitesDemo.class.getResourceAsStream("/db.properties"); 第三种方式:若调用的文件在WEB-INF中时:需要配置Servlet public class properitesServlet extends HttpServlet { private static final long serialVersionUID = 7973003110170094853L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = req.getServletContext();//这里的代码是本类的ServletContext去获得的 InputStream in = context.getResourceAsStream("/WEB-INF/db.properties");//获取相对应的位置文件 Properties p = new Properties();//因为还是properites文件 p.load(in); System.out.println(p.getProperty("uname")); } } 配置: <servlet> <servlet-name>proServlet</servlet-name>//配置的名字 <servlet-class>com.liwangwang.pares.properitesServlet</servlet-class>//绝对路径 </servlet> <servlet-mapping> <servlet-name>proServlet</servlet-name>//配置的名字 <url-pattern>/proServlet</url-pattern>//引用时的地址 </servlet-mapping>xml可以作为文件传输,每一个接口传输的xml节点及节点内容都不相同,怎么用一个xml解析方法解析多种格式的文件
思路:利用递归,从指定节点Element node开始,递归遍历其所有子节点,判断是否还存在子节点,直到没有子节点为止
案例:xml文件是上面那个student.xml
public static void main(final String[] args) throws Exception { Demo test = new Demo(); test.testGetRoot(); } /** * 获取文件的xml对象,然后获取对应的根节点root */ public void testGetRoot() throws Exception { SAXReader sax = new SAXReader();// 创建一个SAXReader对象 File xmlFile = new File("E:\\Y2\\课件\\2、xml解析\\资料\\students.xml");// 根据指定的路径创建file对象 Document document = sax.read(xmlFile);// 获取document对象,如果文档无节点,则会抛出Exception提前结束 Element root = document.getRootElement();// 获取根节点 getNodes(root);// 从根节点开始遍历所有节点 } /** * 从指定节点Element node开始,递归遍历其所有子节点 */ public void getNodes(final Element node) { // 当前节点的名称、文本内容和属性 System.out.println( node.getTextTrim());// 当前节点内容 List<Attribute> listAttr = node.attributes();// 当前节点的所有属性 for ( Attribute attr : listAttr) {// 遍历当前节点的所有属性 String name = attr.getName();// 属性名称 String value = attr.getValue();// 属性的值 System.out.println("属性名称:" + name + "---->属性值:" + value); } // 递归遍历当前节点所有的子节点 List<Element> listElement = node.elements();// 所有一级子节点的list for ( Element e : listElement) {// 遍历所有一级子节点 getNodes(e);// 递归 } } Dom4f合并多个xml文件 SAXReader saxReader = new SAXReader(); Document a = saxReader.read(new File("g:\\taskChain.xml")); //文件一 Document b = saxReader.read(new File("g:\\taskChain2.xml")); //文件二 List<Element> elements = b.getDocument().getRootElement().elements();//获得根节点下的节点信息 Element parent = (Element) a.getRootElement();//获得第一个xml的根节点 for (Element element : elements) { parent.add(element.detach());//将b下的节点添加到a的根节点下 } System.out.println(a.asXML()); XSML中的解析主要的是对每个元素节点的分析: 一些获取的代码: document.selectNodes(xpath);//查一组 document.selectSingleNode(xpath);//查单个