栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 读取本地文件实例详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 读取本地文件实例详解

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;

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;


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();
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号