使用代码创建了一个文件夹和一个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){
}
}
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){
}
}
特别是
result = new StreamResult(pw);
transformer.transform(source, result);
这两行,看看有没有对应的关闭方法
http://www.it.com.cn/f/edu/049/20/26165_2.htm
不是第三方的类库。
import javax.xml.transform.Transformer;
只有这个地方使用了流,而且在代码中试图使用Delete来删除该文件,但是在有的时候删除文件没有成功(但是不能重复出这种环境,试了好几个小时都没有重复出来)
有没有可能时创建文件夹的问题呢?File tmpFilePath = null;
try{
tmpFilePath = new File(TEMPPATH);//TEMPPATH 路径
if(!tmpFilePath.exists()){
tmpFilePath.mkdirs();
}
}catch(Exception ex){
}
刚才我随便写了一个写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");
}
问一下,strXmlPath是不是很长?
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文件能右击也能打开啊
这是文件的名称,应该不能超过长度的限制吧,是使用时间和客户端IP地址组成的名称。
标准的utf-8应该没问题塞。。那你把这句去掉transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8")呢。。我测试的时候也没有用这句。。