java 讀取本地文件實(shí)例詳解
更新時(shí)間:2017年05月04日 08:58:13 投稿:lqh
這篇文章主要介紹了java 讀取本地文件實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
java 讀取本地文件實(shí)例詳解
用javax.xml、w3c解析
實(shí)例代碼:
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為抽象不能直接實(shí)例化(將XML文件轉(zhuǎn)換為DOM文件)
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
// 返回documentBuilderFactory對象
dbf = DocumentBuilderFactory.newInstance();
// 返回db對象用documentBuilderFatory對象獲得返回documentBuildr對象
db = dbf.newDocumentBuilder();
// 得到一個(gè)DOM并返回給document對象
Document dt = db.parse(f);
// 得到一個(gè)elment根元素
element = dt.getDocumentElement();
// 獲得根節(jié)點(diǎn)
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文件內(nèi)容,項(xiàng)目外的路徑
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();
}
//方法二:項(xiàng)目絕對路徑是在本class文件所在項(xiàng)目的根目錄下找,也就是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();
}
//方法五:動態(tài)獲取相對目錄
try {
String xmlPath = "logMonitor.xml";
//獲取當(dāng)前類加載的根目錄,如:/C:/Program Files/Apache/Tomcat 6.0/webapps/fee/WEB-INF/classes/
String path = ReadFileTest.class.getClassLoader().getResource("").toURI().getPath();
// 把文件讀入文件輸入流,存入內(nèi)存中
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();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
SpringBoot項(xiàng)目如何打war包問題詳解
傳統(tǒng)的部署方式:將項(xiàng)目打成war包,放入tomcat的webapps目錄下面,啟動tomcat,即可訪問.文中有非常詳細(xì)的介紹,對正在學(xué)習(xí)springboot的小伙伴很有幫助,需要的朋友可以參考下2021-05-05
基于Spring中的線程池和定時(shí)任務(wù)功能解析
下面小編就為大家?guī)硪黄赟pring中的線程池和定時(shí)任務(wù)功能解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Spring?MVC中@Controller和@RequestMapping注解使用
這篇文章主要介紹了Spring?MVC中@Controller和@RequestMapping注解使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
java中判斷字段真實(shí)長度的實(shí)例(中文2個(gè)字符,英文1個(gè)字符)
下面小編就為大家?guī)硪黄猨ava中判斷字段真實(shí)長度的實(shí)例(中文2個(gè)字符,英文1個(gè)字符)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

