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(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!