public void wordToHtml(String docPath){
String htmlPath=docPath.substring(0, docPath.indexOf("."));
Dispatch.invoke(doc,"SaveAs",Dispatch.Method,
new Object[]{htmlPath,new Variant(8)},new int[1]);
File htmlFile=new File(htmlPath+".htm");
htmlFile.deleteOnExit();
}
public HashMap<Integer,String> getImagesPath(String docPath){
int count=0;
String filePath=docPath.substring(0,docPath.indexOf("."))+".files";
File file=new File(filePath);
HashMap<Integer,String> hm =new HashMap<Integer,String>();
if(file.isDirectory()){
File[] files=file.listFiles();
for(File f:files){
if(f.exists()){
String fileName=f.getName();
String fileSuffix=fileName.substring(fileName.lastIndexOf(".")+1);
if(fileSuffix.equalsIgnoreCase("jpg")||fileSuffix.equalsIgnoreCase("png")){
++count;
if(count%2==0){
hm.put(count/2, f.getAbsoluteFile()+"");
}else{
f.delete();
}
}else{
f.delete();
}
}
}
}
return hm;
}
下面是我写的一个测试类:
public void test(){
ReadDocuments rd=new ReadDocuments();
rd.setVisible(false);
rd.openDoc("D:\试验.docx");
rd.wordToHtml("D:\试验.html");
HashMap hm=rd.getImagesPath("D:\试验.docx");
Iterator<Integer> it=hm.keySet().iterator();
while(it.hasNext()){
System.out.println(hm.get(it.next()));
}
rd.deleteTempFiles("D:\试验.docx");
rd.closeWord();
}
根据API文档,可以得到最直观的两点信息:
delete ()
删除此抽象路径名表示的文件或目录。
可以这样理解,JVM是Java程序运行的环境,当运行test()方法时,JVM随之启动,它也有自己的生命周期。当调用delete()方法直接删除文件时,不用判断文件是否存在,一经调用立即执行;而调用deleteOnExit()方法时,我的理解是只是相当于对deleteOnExit()作一个声明,当程序运行结束,JVM终止时才真正调用deleteOnExit()方法实现删除操作。
在wordToHtml()类中,将word文档转化为html格式时,除生成的html之外,系统还会生成一个.files的文件用来存放word中的图片等相关信息,而.files文件中的图片才是我所要的。当我将其中的deleteOnExit()方法改为delete()方法时会出现删除不干净或者是无法删除html的现象。我的理解是word转化为html是多线程的,多个线程的执行时间长短不一,当某个线程结束出发delete()事件的时候,可能其他的线程仍在执行,顾出现删除不了或无法删除的现象。
相关知识
file的delete()和deleteOnExit()的区别!
C# 模拟提交带附件(input type=file)的表单
基于Spring Boot的宠物健康咨询系统的设计与实现
基于SpringBoot的宠物商城网站的设计与实现
基于SSM的宠物领养收养救助系统的设计与实现
电子商务和新媒体运营的区别(电子商务和网络营销的区别是什么?)
Http POST 提交数据的四种方式解析
沙鼠和仓鼠的区别
真菌和细菌的区别
豚鼠和荷兰猪的区别
网址: file的delete()和deleteOnExit()的区别! https://m.mcbbbk.com/newsview309952.html
上一篇: qq宠物为什么停止运营了,以前的 |
下一篇: qq宠物停止运营 怎么领取停运补 |