功能:
对文件进行解压缩处理代码:
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(sCmd); // sCmd里为解压缩的bat命令,unzip -o %1 -d %2 -nul
pr.waitFor()
}catch (...) {...
}finally {
pr.destroy()
}
----------------------
new file(XXX).delete问题:
程序执行完成后:用程序zip文件没有被删除,但我手动可以删除文件
程序执行中(debug):手动也无法删除文件,提示‘其他用户在使用’我怀疑用程序在做删除文件时,解压缩的线程还存在,所以就不能删除,问题是该如何解决这个问题阿

解决方案 »

  1.   

    首先,debug的时候process还在运行中,还在使用文件,当然不能删除了。
    程序执行结束,文件没有被删除,应该不是process占用着文件,可能是别的原因,因为你用的waitFor,如果process没有执行结束,是不会继续后面的代码执行new file(XXX).delete的,除非这两条语句不在同一个处理中。
    另外,提个醒,用waitFor时候,确实有死锁的可能,javadoc里就有说明,看下面的红色部分的说明java.lang 
    Class Process
    java.lang.Object
      java.lang.Process
    --------------------------------------------------------------------------------public abstract class Processextends ObjectThe ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object. 
      

  2.   

    有错误码吗?new file(XXX)这里,确实有对象创建吗?
      

  3.   

    to:qybao
    在debug下,解压缩完了以后,这个文件是可以删除的。你说的其他地方有可能出错,就是之前还用了个renameTo(),我现在怀疑是不是这个方法有问题,正在调查。to:healer_kx
    无任何错误提示,pr.waitFor()的返回值也是0。newFile(XXX)应该有对象被创建了,否则的话应该报空指针错误吧。我在调试下。。
      

  4.   

    为什么不直接使用 jdk里面的压缩、解压 类呢? 当然只支持zip格式