public class ParseTest{我要用到xml文件的绝对路径,应该怎么做?求指导。
public static void main(String[] args) {
SaxParseService sax = new SaxParseService();
//放在D盘就找不到资源报错了
InputStream input = SaxParseService.class.getClassLoader().getResourceAsStream("D:/book.xml");
//放在src下能正常输出
// InputStream input = SaxParseService.class.getClassLoader().getResourceAsStream("book.xml");
List<Book> books=null;
try {
books = sax.getBooks(input);
} catch (Exception e) {
e.printStackTrace();
}
for(Book book : books){
System.out.println(book.toString());
}
}
}
Java XML
------解决方案--------------------
InputStream input = SaxParseService.class.getClassLoader().getResourceAsStream("D:/book.xml");
改成
InputStream input = new FileInputStream("D:/book.xml");
试试看?
------解决方案--------------------
好好学习下getClassLoader()是什么意思