java 读取本地文件实例详解
程序员文章站
2024-02-26 17:19:31
java 读取本地文件实例详解
用javax.xml、w3c解析
实例代码:
package cn.com.xinli.monitor.utils;...
java 读取本地文件实例详解
用javax.xml、w3c解析
实例代码:
package cn.com.xinli.monitor.utils; import org.w3c.dom.document; import org.w3c.dom.element; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import java.io.file; /** * created by jiyy on 2017/4/6. */ public class readxmltest { public static void main(string[] args){ element element = null; // 可以使用绝对路劲 file f = new file("d:/workspace-idea/monitor-service/src/main/resources/logmonitor.xml"); // documentbuilder为抽象不能直接实例化(将xml文件转换为dom文件) documentbuilder db = null; documentbuilderfactory dbf = null; try { // 返回documentbuilderfactory对象 dbf = documentbuilderfactory.newinstance(); // 返回db对象用documentbuilderfatory对象获得返回documentbuildr对象 db = dbf.newdocumentbuilder(); // 得到一个dom并返回给document对象 document dt = db.parse(f); // 得到一个elment根元素 element = dt.getdocumentelement(); // 获得根节点 system.out.println("根元素:" + element.getnodename()); }catch (exception e ){ e.printstacktrace(); } } }
用dom4j解析
package cn.com.xinli.monitor.test; import org.apache.commons.io.ioutils; import org.dom4j.document; import org.dom4j.documentexception; import org.dom4j.documenthelper; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.net.urisyntaxexception; /** * created by jiyy on 2017/4/6. */ public class readfiletest { public static void main(string[] args){ //方法一:本地绝对路径获取xml文件内容,项目外的路径 string fileurl = "/d:/workspace-idea/monitor-service/src/main/resources/logmonitor.xml"; inputstream fis = null; try { fis = new fileinputstream(new file(fileurl)); string content = ioutils.tostring(fis,"utf-8"); document document = documenthelper.parsetext(content); } catch (java.io.ioexception e) { e.printstacktrace(); } catch (documentexception e) { e.printstacktrace(); } //方法二:项目绝对路径是在本class文件所在项目的根目录下找,也就是classes/下 try { string content2 = ioutils.tostring(readfiletest.class.getresourceasstream("/logmonitor.xml"), "utf-8"); document document2 = documenthelper.parsetext(content2); } catch (ioexception e) { e.printstacktrace(); }catch (documentexception e) { e.printstacktrace(); } //方法三:相对目录,在本readfiletest编译后的.class文件同级目录 try { string content3 = ioutils.tostring(readfiletest.class.getresourceasstream("logmonitor.xml"), "utf-8"); document document3 = documenthelper.parsetext(content3); } catch (ioexception e) { e.printstacktrace(); }catch (documentexception e) { e.printstacktrace(); } //方法四:相对目录,在本readfiletest编译后的.class文件上级目录的config目录下 try { string content4 = ioutils.tostring(readfiletest.class.getresourceasstream("../config/logmonitor.xml"), "utf-8"); document document4 = documenthelper.parsetext(content4); } catch (ioexception e) { e.printstacktrace(); }catch (documentexception e) { e.printstacktrace(); } //方法五:动态获取相对目录 try { string xmlpath = "logmonitor.xml"; //获取当前类加载的根目录,如:/c:/program files/apache/tomcat 6.0/webapps/fee/web-inf/classes/ string path = readfiletest.class.getclassloader().getresource("").touri().getpath(); // 把文件读入文件输入流,存入内存中 fileinputstream in = new fileinputstream(new file(path + xmlpath)); string content5 = ioutils.tostring(in,"utf-8"); document document5 = documenthelper.parsetext(content5); } catch (ioexception e) { e.printstacktrace(); } catch (documentexception e) { e.printstacktrace(); }catch (urisyntaxexception e) { e.printstacktrace(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: vs2015 C# 控制台简单SpringDEMO
下一篇: Python中查看文件名和文件路径