如下代码所示,我希望分析test文件夹下的xml,现在又5个xml文件,执行下面代码后,结果如下。由于文件ddmk_weibo_fenxishi_list_16944905_3098730807.xml,不能正确读取,我希望进行删除。在catch里执行filelist[i].delete(),但是执行后该文件未被删除。
请问该怎么解决呢?
package myproject; import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; public class xmlValidate { public static void main(String[] args) { String directory="C:/Users/long/DataScraperWorks/test"; String fileSuffix = ".xml"; File file = new File(directory); File[] filelist = file.listFiles(); for(int i=0;i<filelist.length;i++){ if(filelist[i].getName().lastIndexOf(fileSuffix)!=-1){ String filename = filelist[i].getName(); SAXReader reader = new SAXReader(); try { Document document = reader.read(directory+"/"+filename); System.out.println(filelist[i].getName()+" Load success!"); //filelist[i].delete(); } catch (DocumentException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); System.out.println(filelist[i].getName()+" load fail"); filelist[i].delete(); } } } } }输出结果:
试试 FileUtils.forceDelete(...) 方法, commons-io 项目里的
######谢谢。我去试试。 主要我想在对文件进行判断后,删除抛出异常的文件!希望Commons-IO能解决这个删除问题!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。