java 讀取本地文件實例詳解
用javax.xml、w3c解析
實例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
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(); } } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/jiyingying_up/article/details/69396501