java怎样读取本地文件_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;

/**

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

}

}

}

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值