java写入xml数据,在其他电脑上运行这方法是正常的,但是在自己电脑上就运行错误了,找了很久没有找出啥原因,下午大神帮帮忙。
错误在((org.apache.crimson.tree.XmlDocument)document).write(fos);这句代码,不知道为什么。
异常如下图1:
源码如下:
/**
* 修改属性值
* @param fileName
* @param nodeName
* @param update
* @throws InterruptedException
* @throws IOException
*/
public static void UpdataXml(String fileName,String nodeName,String update) throws IOException, InterruptedException{
String path = System.getProperty("user.dir") + "\\params\\"+fileName+".xml";
FileReader fr = new FileReader(path);
// if (fr.read()==-1) {
System.out.println("新建文件....");
Deal.CreateXml("data", nodeName, fileName);
// }
// }else if(fr.read()!=-1 ) {
System.out.println("文件内容不为空");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
Document document=db.parse(path);
Node root=document.getDocumentElement();
System.out.println(root);
//判断文件内容是否为空
NodeList nodeList = root.getChildNodes();
for (int i=0;i<nodeList.getLength();i++) {
Node subnode = nodeList.item(i);
if (subnode.getNodeName()==nodeName) {
subnode.getFirstChild().setNodeValue(update);
}
System.out.println("节点已经更改");
}
//写入数据
System.err.println("数据保存地址为:"+path);
FileOutputStream fos=new FileOutputStream(new File(path));
try {
((org.apache.crimson.tree.XmlDocument)document).write(fos);
System.err.println("程序已将数据保存");
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
// TODO: handle exception
}
}