无法通过java或手动删除文件

无法通过java或手动删除文件,java,file,io,delete-file,Java,File,Io,Delete File,我有以下情况,在servlet中创建一个文件,然后必须删除它。 在执行该文件时,我发现该文件仍在服务器中,因此我尝试手动删除它,但无法,我收到以下消息: 此文件由另一个程序打开:javaw.exe 这是我的密码: public class GenerateFile extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletReq

我有以下情况,在servlet中创建一个文件,然后必须删除它。 在执行该文件时,我发现该文件仍在服务器中,因此我尝试手动删除它,但无法,我收到以下消息:

此文件由另一个程序打开:javaw.exe

这是我的密码:

public class GenerateFile extends Action { 
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ok");
        String fileName = request.getParameter("fileName");
        Integer nbrParam = Integer.parseInt(request.getParameter("nbrParam"));
        String[] valueParam = new String[nbrParam+1];
        for(int i =1;i<=nbrParam;i++)
        {  System.out.println(request.getParameter("param"+i));
            valueParam[i]=request.getParameter("param"+i);
        }
        FileInputStream in = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\"+fileName+".doc");
        POIFSFileSystem fs = new POIFSFileSystem(in);
        HWPFDocument doc = new HWPFDocument(fs);
        Range r = doc.getRange();
        for(int i=1;i<=nbrParam;i++)
        {   System.out.println("<param"+i+">");
            System.out.println(valueParam[i]);
            r.replaceText("<param"+i+">", valueParam[i]);
        }
        File  file = new File("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp");
        File temp = File.createTempFile("monfile",".doc",file);
        String tempName =temp.getName();
        doc.write( new FileOutputStream(temp));
        OutputStream out = response.getOutputStream();
        response.setContentType("application/rtf");
        response.setHeader("Content-Disposition","attachment; filename=Decision");
        FileInputStream in1 = new FileInputStream(temp);
        byte[] buffer = new byte[4096];
        int length;

        while ((length = in1.read(buffer)) > 0){
            out.write(buffer, 0, length);
        }
        in1.close();
        out.flush();
        System.out.println("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\"+tempName);
        File f = new File("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\"+tempName);
        f.delete();

        return null;
    }
}
公共类GenerateFile扩展操作{
公共ActionForward执行(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求、HttpServletResponse响应)引发IOException{
System.out.println(“ok”);
字符串文件名=request.getParameter(“文件名”);
整数nbrParam=Integer.parseInt(request.getParameter(“nbrParam”);
字符串[]valueParam=新字符串[nbrParam+1];
对于(int i=1;i 0){
out.write(缓冲区,0,长度);
}
in1.close();
out.flush();
System.out.println(“C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\”+tempName);
文件f=新文件(“C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\”+tempName);
f、 删除();
返回null;
}
}

您应该关闭所有文件读取对象实例。此外,如果您可以手动删除该文件,则应先关闭java,然后再将其删除,javaw是在控制台外启动java的过程。

似乎您没有关闭该文件(out),因此该文件仍保留在该操作的线程中,该线程限制该文件被删除


希望有帮助。

也许您应该尝试找出哪个进程确切地保存打开的文件

问题是您正在创建一个
新文件outputstream(tempName)
来写入该文件,但从未关闭该outputstream(或与之链接的另一个outputstream)

这样做:

FileOutputStream fos = newFileOutputStream(tempName);
// use it
fos.close(); // CLOSE IT!!

// then you can delete the file
简化

也许你可以用另一种方式,不用临时文件

例如:
doc.write(新文件输出流(tempName))
可以替换为:

doc.write(response.getOutputStream());
这样,doc将其字节直接发送到您需要的位置,而不是发送到临时文件,从而消除了对它的需要

输入/输出流背后的理念是组合它们。输入/输出流是抽象基类。并且有很多实现:

  • 基于内存:ByteArrayInput/OutputStream
  • 基于文件:FileInputOutStream
  • 压缩/解压缩到另一个输出流:GZIPInputOutStream
  • 等等
它的美妙之处在于应用decorator模式来添加功能。举例来说:

new GZipOutputStream(new ByteArrayOutputStream());

// creates an outputstreams that compress data received and send it to the other stream
// the BAOS then writes the received bytes to memory


new GZipOutputStream(new FileOutputStream());
// it's the same but sending compressed bytes to a file.

对于IO特性,我建议使用社区已经提供的某种jar。 例如,common-io.x-x.jar、spring-core.jar

    Eg, org.apache.commons.io.FileUtils;
        FileUtils.copyDirectory(from, to);
        FileUtils.deleteDirectory(childDir);
        FileUtils.forceDelete(springConfigDir);
        FileUtils.writeByteArrayToFile(file, data);

        org.springframework.util.FileSystemUtils;
        FileSystemUtils.copyRecursively(from, to);
        FileSystemUtils.deleteRecursively(dir);

祝你好运

无论何时打开文件处理程序,都应将其关闭。在希望长时间运行的Java应用程序中,强烈建议您在使用完所有未使用的文件处理程序后立即关闭它们

常见文件处理程序的示例有
FileOutputStream
FileInputstream
。下面是一个如何打开和关闭
FileOutputStream

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(tempName);
    // do something
} catch (IOException ex) {
    // deal with exceptions
} finally {
    // close if fos is not null
    if (fos != null) {
        fos.close();
    }
}
你不应该这样做:

doc.write( new FileOutputStream(temp));

因为如果文件处理程序没有引用,您永远无法关闭它。

看在皮特的份上,修复您的代码格式。。。(如果您已经格式化了代码,只需将其复制并粘贴到此处,选择它并点击
Ctrl+k
,您就无法删除正在Windows下使用/打开的文件。您必须等待该文件不再使用。感谢您给出了这个好的、解释良好且慷慨的答案!关于简化id,请这样做:OutputStream out=response.getOutputStream();doc.write(out);out.flushi在我的页面上获取一些stange字符,而不是保存dialog@fatiDev:告诉我您做了什么更改?只是关闭文件的输出?我会尝试
doc.write(response.getOutputStream())
方法(以消除机会),但在设置所有响应头之前不要写入:)这是因为当您编写响应头部分的主体时,它会自动“提交”(完成并发送给用户)。因此,先做标题,然后写文档。