Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1037683
  • 博文数量: 155
  • 博客积分: 5339
  • 博客等级: 大校
  • 技术积分: 1436
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-10 21:41
文章分类

全部博文(155)

文章存档

2016年(3)

2015年(7)

2014年(3)

2013年(1)

2012年(8)

2011年(5)

2010年(1)

2009年(5)

2008年(4)

2007年(26)

2006年(46)

2005年(46)

分类: Java

2005-11-01 14:14:33

关于偷懒的错误

            System.out.println("--gen xml file .....");
            java.io.File file=new java.io.File(filepath);
            TransformerFactory tf=TransformerFactory.newInstance();
            Transformer transformer=tf.newTransformer();
            DOMSource source=new DOMSource(output);
            java.io.FileOutputStream fos=new java.io.FileOutputStream(file);
            StreamResult result=new StreamResult(fos);
            Properties props = new Properties();
            props.setProperty("encoding", "GB2312");
            props.setProperty("method", "xml");
            props.setProperty("omit-xml-declaration", "yes");
            transformer.setOutputProperties(props);
            transformer.transform(source,result);
            fos.close();
            System.out.println("--end gen xml file.");

            System.out.println("--gen xml file .....");
            java.io.File file=new java.io.File(filepath);
            TransformerFactory tf=TransformerFactory.newInstance();
            Transformer transformer=tf.newTransformer();
            DOMSource source=new DOMSource(output);
            StreamResult result=new StreamResult(file);
            Properties props = new Properties();
            props.setProperty("encoding", "GB2312");
            props.setProperty("method", "xml");
            props.setProperty("omit-xml-declaration", "yes");
            transformer.setOutputProperties(props);
            transformer.transform(source,result);
            System.out.println("--end gen xml file.");

      就因为我把上面三行代码做的事情写成了一行StreamResult result=new StreamResult(file);

于是我始终是不能对file进行renameTo操作或者delete操作,如果开始我能发现这一点,还是值得

庆幸的,但事实上我花了很长时间来找出问题,我甚至怀疑是Weblogic的限制所致,因为下面的程序

在Tomcat里运行是没有问题的,但现在想来有可能是使用的TransformerFactoryImpl不同造成的,

Weblogic使用的是org.apache.xalan.xsltc.trax.TransformerFactoryImpl ,而Tomcat用的是jdk

默认的。后来,我尝试了很多办法来解决这个问题,最终问题指向了写入文件的输出流,我并没有显

示控制文件的输出流,天知道StreamResult(file)作了什么,用java.io.FileOutputStream fos控制对

文件输入,并最终关闭文件输出流,这样做了之后,再也没有什么后遗症了,ok!这种错误以后应当

引以为戒。不过为什么在Tomcat中的运行完全没有问题,这其中是何道理呢??

阅读(1350) | 评论(0) | 转发(0) |
0

上一篇:ASCII字码表

下一篇:看不看由你

给主人留下些什么吧!~~