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



