不知道错在哪显示找不到信息
系统找不到文件??
package com.imooc.dom4j;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrWriter {
public void writeXml() {
String file = "e:/DESKTOP/JavaWeb/workspace/xml/src/hr.xml";
SAXReader reader = new SAXReader();
try {
Document document = reader.read("file");
// 获取根节点
Element root = document.getRootElement();
// 创建新的根节点
Element employee = root.addElement("employee");
employee.addAttribute("no", "104");
Element name = employee.addElement("name");
/*System.out.println("请输入你的名字");
Scanner sc = new Scanner(System.in);
String s = sc.next();*/
name.setText("李铁柱");
// 简便写法
employee.addElement("age").setText("20");
Element sex = employee.addElement("sex");
sex.setText("女");
employee.addElement("salary").setText("5000");
// 多重节点
Element department = employee.addElement("department");
department.addElement("dname").setText("会计部");
department.addElement("daddress").setText("xx大厦-308");
// 最后还要用到输入输出流的方法
FileOutputStream fo = new FileOutputStream(file);
Writer writer;
writer = new OutputStreamWriter(fo, "UTF-8");
// document.writer
document.write(writer);
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HrWriter h = new HrWriter();
h.writeXml();
}
}
报错代码信息
org.dom4j.DocumentException: E:\DESKTOP\JavaWeb\workspace\xml\file (系统找不到指定的文件。)
at org.dom4j.io.SAXReader.read(SAXReader.java:464)
at org.dom4j.io.SAXReader.read(SAXReader.java:303)
at com.imooc.dom4j.HrWriter.writeXml(HrWriter.java:23)
at com.imooc.dom4j.HrWriter.main(HrWriter.java:68)
Caused by: java.io.FileNotFoundException: E:\DESKTOP\JavaWeb\workspace\xml\file (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:445)
... 3 more
正在回答
如下图所示,建议同学将file变量的双引号去掉,如果你给file加上双引号,就不是存储路径的变量了,就变成字符串了,祝学习愉快~
根据报错提示来看,在E:\DESKTOP\JavaWeb\workspace\xml\file这个路径下找不到需要读取的hr.xml文件,解决方法,建议你检查一下hr.xml文件是否存在上述路径中,如果存在,你也可以使用相对路径来获取hr.xml文件,譬如:String file = "src/hr.xml",如果我的建议解决了你的问题,请采纳,祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星