服务器如何读取文件

来源:互联网 发布:js在线格式化工具 编辑:程序博客网 时间:2024/06/05 02:25

首先取得路径,我这边没办法通过session取,只能取当前class的路径,然后拼接下

String url = GetBipURL.class.getResource("").toURI().getPath().toString();url = url.substring(1);String []temp = url.split("WEB-INF");if (temp.length > 0) {result = temp[0] + "WEB-INF" + File.separator + "classes" + File.separator + "META-INF"+ File.separator + "common"  +  File.separator;}

然后读取文件

//创建SAXReader对象  SAXReader reader = new SAXReader();  //读取文件 转换成Document  Document document = reader.read(new File(File.separator + result + "xxConfig.xml"));  //获取根节点元素对象  Element root = document.getRootElement();
这是遍历xml文件的方法

public static void listNodes(Element node) {// 如果当前节点内容不为空,则输出if (!node.getTextTrim().equals("")) {element.put(node.getName(), node.getText());//System.out.println(node.getName() + ":" + node.getText());}// 同时迭代当前节点下面的所有子节点// 使用递归@SuppressWarnings("unchecked")Iterator<Element> iterator = node.elementIterator();while (iterator.hasNext()) {Element e = iterator.next();listNodes(e);}}
类里面定义两个静态变量

private static HashMap<String, String> element = new HashMap<>();public static String bipUrl = StringUtil.EMPTY;







0 0
原创粉丝点击