使用代码创建了一个文件夹和一个xml文件,但是生成的xml文件不能点击右键(文件夹也不能点击右键),会出现系统错误。即使程序关闭,也不能点击,必须关掉服务器,估计可能没有完全释放掉占用的资源,但不知道错在了哪里。求各位大侠帮帮忙,小弟先谢谢了!                  TransformerFactory tf = TransformerFactory.newInstance();
OutputStreamWriter pw = null;
FileOutputStream fs = null;
StreamResult result = null;
try {
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
                    fs = new FileOutputStream(strXmlPath);//strXmlPath为路径
pw = new OutputStreamWriter(fs,"utf-8");
result = new StreamResult(pw);
transformer.transform(source, result);
fs.flush();
pw.flush();
fs.close();
pw.close();
}
catch (Exception ex) {
}finally{
try{
if(null!=fs){
fs.close();
}
}catch(Exception ex){
}

try{
if(null!=pw){
pw.close();
}
}catch(Exception ex){
}
}

解决方案 »

  1.   

    使用了第三方的类库?是不是第三方的类库也需要关闭
    特别是 
    result = new StreamResult(pw);
    transformer.transform(source, result); 
    这两行,看看有没有对应的关闭方法
      

  2.   

    你确定 是你这段程序的问题? 这段程序应该不会引起那个问题吧检查一下是否是与之关联的程序片段引起的可以看看这里的应用
    http://www.it.com.cn/f/edu/049/20/26165_2.htm
      

  3.   


    不是第三方的类库。
    import javax.xml.transform.Transformer;
      

  4.   


    只有这个地方使用了流,而且在代码中试图使用Delete来删除该文件,但是在有的时候删除文件没有成功(但是不能重复出这种环境,试了好几个小时都没有重复出来)
    有没有可能时创建文件夹的问题呢?File tmpFilePath = null;
    try{
    tmpFilePath = new File(TEMPPATH);//TEMPPATH 路径
    if(!tmpFilePath.exists()){
        tmpFilePath.mkdirs();
    }
    }catch(Exception ex){
    }
      

  5.   


    刚才我随便写了一个写xml的类。。没有你说的情况。。test.xml可以右键也可正常打开// 测试LZ问题
    public static void writeXMLFile() throws Exception {
    Document document = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory
    .newDocumentBuilder();
    // 下面是建立XML文档内容的过程
    document = documentBuilder.newDocument(); 
    Element root = document.createElement("webdisknodes");
    // 根元素添加上文档
    document.appendChild(root); 

    // 建立node元素,添加到次级根结点
    Element resultNode = document.createElement("node");
    root.appendChild(resultNode); // name
    Element method = document.createElement("test");
    resultNode.appendChild(method);
    Text tMethod = document.createTextNode("测试第一个节点");
    method.appendChild(tMethod);
    Element methodArgs = document.createElement("second");
    resultNode.appendChild(methodArgs);
    Text tMethodArgs = document.createTextNode("测试第二个节点");
    methodArgs.appendChild(tMethodArgs);  TransformerFactory tfactory = TransformerFactory.newInstance();
    // 对document对象调用normalize(),去掉xml文档中格式
    Transformer transformer = tfactory.newTransformer(); 
    document.normalize();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File("f:/test.xml"));
    transformer.transform(source, result);
    System.out.println("success");
    }
      

  6.   

    是啊,正常创建的xml文档没有问题,也不知道是那里错了,今天机器重启后对创建的文件夹或者xml文件还是不能点击右键,服务器还没有启动呢,不知道是不是创建文件夹和xml文件本身的问题。还在考虑中~~~
      

  7.   

    刚才试了试把xml的后缀名变为txt(因为生成的xml不能进行右键及拷贝删除等操作),拷贝到其他的地方,再把文件变为xml后缀名,这个文件还是不能点击右键,出现一样的情况。但是通过把文件里的内容改动一点点文件就正常的可用了,比如加一个空格什么的,只要对文件有改变,xml就正常的可用。这是什么原因呢?
      

  8.   

    sounds strange。
    问一下,strXmlPath是不是很长?
      

  9.   


    TransformerFactory tf = TransformerFactory.newInstance();
    OutputStreamWriter pw = null;
    FileOutputStream fs = null;
    StreamResult result = null;
    String strXmlPath="c:/testXML.xml";
    try {
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    fs = new FileOutputStream(strXmlPath);//strXmlPath为路径
    pw = new OutputStreamWriter(fs,"utf-8");
    result = new StreamResult(pw);
    transformer.transform(source, result);
    fs.flush();
    pw.flush();
    fs.close();
    pw.close();
    }
    catch (Exception ex) {
    }finally{
    try{
    if(null!=fs){
    fs.close();
    }
    }catch(Exception ex){
    } try{
    if(null!=pw){
    pw.close();
    }
    }catch(Exception ex){
    }
    }
    }
    我只改了strXmlPath,运行后生成的xml文件能右击也能打开啊
      

  10.   

    文件内容长,没关系,但路径名不能太长,各操作系统都有文件名长度限制。windows是256字节,超过这个长度后,内容可以保存,但是右击等操作都会有问题。
      

  11.   

    132187139373310.10.72.172.xml
    这是文件的名称,应该不能超过长度的限制吧,是使用时间和客户端IP地址组成的名称。
      

  12.   

    试过了,仍然不是这个问题,关键是把生成的xml文档转换为txt,在里面随便改点什么(如:在最后添加一个空格),在改回来xml类型的就可以正常的右键使用了,可不可能是编码格式的问题呢?
      

  13.   


    标准的utf-8应该没问题塞。。那你把这句去掉transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8")呢。。我测试的时候也没有用这句。。