本文只是基於FileOuutPut和this.getClass().getResourceAsSream()做的測試。
package com.eblly.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class BookXml {
/*
* 獲取book.xml所有信息 ,幷封裝成對象
*/
public List<Book> findAll(){
InputStream is = null;
//需要返回的書單
List<Book> bookList = new ArrayList<>();
try{
// is = new FileInputStream("/home/eblly/program/java/eclipse/xmlhomework0704/src/book.xml"); //使用絕對路徑可以讀得出
File file = new File("src/book.xml"); //輸出File裏面的String參數
// System.out.println(file);
// is = new FileInputStream("src/book.xml"); //可以輸出
// is = new FileInputStream("/src/book.xml"); //java.io.FileNotFoundException: /src/book.xml
// is = new FileInputStream("./src/book.xml"); //可以輸出
// is = new FileInputStream("./book.xml"); //java.io.FileNotFoundException: ./book.xml
// is = new FileInputStream("/book.xml"); //java.io.FileNotFoundException: /book.xml
// is = new FileInputStream("book.xml"); //java.io.FileNotFoundException: book.xml
//這個使用相對路徑即可
// is = this.getClass().getResourceAsStream("/book.xml"); //可以輸出
// is = this.getClass().getResourceAsStream("src/book.xml"); //null
// is = this.getClass().getResourceAsStream("./book.xml"); //null
// is = this.getClass().getResourceAsStream("book.xml"); //null
// System.out.println(this.getClass()); //class com.eblly.xml.BookXml
// System.out.println(this.getClass().getResource("/")); //file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/
// System.out.println(this.getClass().getResource("./"));//file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/com/eblly/xml/
// System.out.println(this.getClass().getResource("/.."));//null
// System.out.println(this.getClass().getResource("."));//file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/com/eblly/xml/
// System.out.println(this.getClass().getResource("../"));//file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/com/eblly/
// System.out.println(this.getClass().getResource("../../"));//file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/com/
// System.out.println(this.getClass().getResource("../../../"));//file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/
// System.out.println(this.getClass().getResource("../../../../"));//null
// is = this.getClass().getResourceAsStream("/xml/book.xml"); //可以
/*
* FileInputStream是指向/home/eblly/program/java/eclipse/xmlhomework0704,
* 而this.getClass()是指向運行文件的目錄,即/home/eblly/program/java/eclipse/xmlhomework0704/bin
*
* 如果把book.xml放到xml文件夾裏面,而xml文件是xmlhomework0704的子目錄的話,is = new FileInputStream("src/book.xml")需要
* 把src改成xml. 而getResourceAsStream則無法獲得book.xml。
* 如果把book.xml放到xml文件夾裏面,而xml文件是src的子目錄的話,,is = new FileInputStream("xml/book.xml")可以獲得文件,
* is = this.getClass().getResourceAsStream("/xml/book.xml")也可以獲得文件。
*
* 而 this.getClass().getResourceAsStream("/book.xml")的“根目錄”就是/home/eblly/program/java/eclipse/xmlhomework0704/bin,
* 所以再怎麼上一層目錄還是不變。如果xml文件夾在src下,book.xml在xml文件夾中,使用/xml/book.xml可以獲得文件。
*
* FileInputStream和this.getClass().getResourceAsStream("/book.xml")還有一個區別在於根目錄"/"的寫入與否。FileInputStream一系統
* 根目錄爲根目錄,getResourceAsStream是以bin爲根目錄。
*
*/
// URL str = this.getClass().getResource("/book.xml"); //file:/home/eblly/program/java/eclipse/xmlhomework0704/bin/book.xml
// System.out.println(is);
Document document = new SAXReader().read(is);
Element root = document.getRootElement();
//書裏面的元素
List<Element> elementList = root.elements();
for(Element e : elementList){
Book book = new Book();
book.setId(e.elementText("id"));
book.setName(e.elementText("name"));
book.setAuthor(e.elementText("author"));
book.setPrice(e.elementText("price"));
book.setImage(e.elementText("image"));
book.setRemark(e.elementText("remark"));
bookList.add(book);
}
}catch (Exception e){
e.printStackTrace();
}finally{
try{
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
return bookList;
}
public static void main(String[] args){
BookXml bookxml = new BookXml();
List<Book> list = bookxml.findAll();
for(Book book : list){
System.out.println("id號:" + book.getId());
System.out.println("書名:" + book.getName());
System.out.println("價格:" + book.getPrice());
System.out.println("相片:" + book.getImage());
System.out.println("簡介:" + book.getRemark());
System.out.println();
}
}
}
項目在eclipse中的文件目錄如下:
在硬盤上,項目的文件目錄如下:
FileOutPutStream和FileInPutStream是一樣的。本文是對xml文件進行操作,需要導入dom4j包。
注:在eclipse中,它會自動把你的src文件夾生成一個bin文件夾,幷把裏面所有的.java編譯成.class文件,src裏面的其他文件則是原樣複製。
這僅僅是在eclipse裏才是這樣,如果你使用vim或者editplus編譯,則需要自己使用命令編譯。
java路徑問題。