显示java io FileNotFoundException
# 具体遇到的问题
显示java io FileNotFoundException, 老师帮我看看这是啥原因吧,我用附加的测试代码咋也不行。。。
# 报错信息的截图
D:/Program%20Files%20(x86)/workspace/mgallery/build/classes/painting.xml
java.io.FileNotFoundException: D:\Program%20Files%20(x86)\workspace\mgallery\build\classes\painting.xml (系统找不到指定的路径。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.imooc.mgallery.utils.XmlDataSource.append(XmlDataSource.java:88)
at com.imooc.mgallery.utils.XmlDataSource.main(XmlDataSource.java:118)
/D:/Program%20Files%20(x86)/workspace/mgallery/build/classes/painting.xml
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
package com.imooc.mgallery.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.imooc.mgallery.entity.Painting;
/**
* 数据源类,用于将XML文件解析为Java对象
* @author LENOVO
*
*/
public class XmlDataSource {
//通过static静态关键字保证数据全局唯一
private static List<Painting> data = new ArrayList();
private static String dataFile;
static {
//得到类路径文件下的painting.xml的地址
//c:\new style\painting.xml
//空格被转化为%20
dataFile = XmlDataSource.class.getResource("/painting.xml").getPath();
reload();
}
private static void reload() {
URLDecoder decoder = new URLDecoder();
try {
decoder.decode(dataFile,"UTF-8");
System.out.println(dataFile);
//利用dom4j对xml进行解析
SAXReader reader = new SAXReader();
//1.获取document文档对象
Document document = reader.read(dataFile);
//2.Xpath得到xml的节点集合
List<Node>nodes = document.selectNodes("/root/painting");
data.clear();
for(Node node:nodes) {
Element element = (Element)node;
String id = element.attributeValue("id");
String pname = element.elementText("pname");
Painting painting = new Painting();
painting.setId(Integer.parseInt(id));
painting.setPname(pname);
painting.setCategory(Integer.parseInt(element.elementText("category")));
painting.setPrice(Integer.parseInt(element.elementText("price")));
painting.setPreview(element.elementText("preview"));
painting.setDescription(element.elementText("description"));
data.add(painting);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取所有油画Painting对象
* @return Painting List
*/
public static List<Painting> getRawData(){
return data;
}
public static void append(Painting painting) {
//1.读取xml文档,得到document对象
SAXReader reader = new SAXReader();
Writer writer = null;
try {
Document document = reader.read(dataFile);
//2.创建新的painting节点
Element root = document.getRootElement();//<root>
Element p = root.addElement("painting");
//3.创建painting节点的各个子节点
p.addAttribute("id", String.valueOf(data.size()+1));
p.addElement("pname").setText(painting.getPname());
p.addElement("category").setText(painting.getCategory().toString());
p.addElement("price").setText(painting.getPrice().toString());
p.addElement("preview").setText(painting.getPreview());
p.addElement("description").setText(painting.getDescription());
writer = new OutputStreamWriter(new FileOutputStream(dataFile),"UTF-8");
document.write(writer);
// System.out.println(dataFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(writer!=null) {
try {
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
reload();//重新加载,保证内存与文件的数据一致
}
}
public static void main(String[]args) {
//new XmlDataSource();
// List<Painting> ps = XmlDataSource.getRawData();
// System.out.println(ps);
Painting p =new Painting();
p.setPname("测试油画");
p.setCategory(1);
p.setPrice(4000);
p.setPreview("/upload/10.jpg");
p.setDescription("测试油画描述");
XmlDataSource.append(p);
}
}
正在回答
同学你好,1. 建议同学在代码中添加下方代码试一下
URLDecoder decoder = new URLDecoder();
dataFile= decoder.decode(dataFile,"UTF-8");
然后清除缓存。如下所示:
2. 如果还存在问题,则建议同学将项目放置到没有空格路径中试一下(如:直接放置到D盘)。
- 参与学习 人
- 提交作业 9401 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星