无法在文件存在的情况下使用IO或NIO删除文件。

huangapple go评论74阅读模式
英文:

Can't delete a file using IO or NIO while file exists

问题

以下是翻译好的内容:

在第一种方法中,我创建了一个需要在处理后删除的文件。为此,我使用了输入流。处理完成后,名为fileSrc的文件必须从硬盘中删除。

我尝试了两种不同的方法来删除文件。

public File signPDF(File fileSrc) {
    // 签名处理 ...

    boolean isDeleted = fileSrc.delete();
    boolean isFileExist = fileSrc.exists();
    logger.info("文件是否存在?- " + isFileExist);
    if (isDeleted) {
        logger.info("文件已删除");
    } else {
        logger.warn("文件无法删除");
    }
    // 尝试在 Java 退出时删除
    Path path = Paths.get(fileSrc.toString());
    try {
        isFileExist = Files.deleteIfExists(path);
        logger.info("使用NIO删除文件:" + isFileExist);
    } catch (IOException e) {
        // 删除文件失败
        logger.warn("使用NIO删除文件失败:" + isFileExist);
        e.printStackTrace();
    }
    
    return signedFile;
}

在我的日志中,我注意到文件没有被删除。
我在另一个方法中通过将输入流复制到另一个文件来下载该文件。经典方法:

FileUtils.copyInputStreamToFile(inputStream, targetFile);

我尝试了几种关闭连接和输入流的方法:

IOUtils.close(con);
inputStream.close();
IOUtils.closeQuietly(inputStream);

不幸的是,这些方法都不起作用。

附注:通过修改文件路径(不正确的路径),此问题已得到解决,感谢 @GyroGearless。最终,我遇到了第二个问题。文件在处理之前被删除了。
我使用以下代码片段解决了这个问题:

File toDelete = new File(fileSrc + File.separator + docId + ".pdf");
try {
    TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
    logger.error("InterruptedException:" + e.getMessage());
    e.printStackTrace();
}
toDelete.deleteOnExit();
英文:

In a first method I create a file that i need to delete after it has been processed. For this I use an input stream. After treatment, the file, named fileSrc has to be deleted from hard drive.

I tried two different way to delete the file.

public File signPDF(File fileSrc) {
    // signature process ... 

    boolean isDeleted = fileSrc.delete();
    boolean isFileExist = fileSrc.exists();
    logger.info("Is file exist ? - " + isFileExist);
    if(isDeleted) {
        logger.info("File has been deleted");
    } else {
        logger.warn("File could not be deleted");
    }
    // try to delete on java exit
    Path path = Paths.get(fileSrc.toString());
    try {
        isFileExist = Files.deleteIfExists(path);
        logger.info("File has been deleted with NIO: " + isFileExist);
    } catch (IOException e) {
        //deleting file failed
        logger.warn("File could not be deleted with NIO: " + isFileExist);
        e.printStackTrace();
    }
    
    return signedFile
}

In my log, i note that the file is not deleted.
I donwload the file copying an inputstream in another method. Classically:

FileUtils.copyInputStreamToFile(inputStream, targetFile);

I tried several methods to close the connection and the inputStream

 IOUtils.close(con);
 inputStream.close();
 IOUtils.closeQuietly(inputStream);

None of those methods work unfortunatelly.

Note to complete : This issue was solved modifying the file path (uncorrect) thanks to @GyroGearless. Finally I met a second issue. THe file was deleted before being handled.
I solved this with this code snippet :

File toDelete = new File(fileSrc + File.separator + docId + ".pdf");
try {
    TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
    logger.error("InterruptedException "  + e.getMessage());
    e.printStackTrace();
}
toDelete.deleteOnExit();

答案1

得分: 2

因为有两个原因,当文件被另一个应用程序打开或者您的应用程序没有删除文件的权限时,您不能首先删除该文件。

英文:

There are two reasons why you cannot delete the file first when it is open by another application or if your application does not have permission to delete the file.

huangapple
  • 本文由 发表于 2020年9月24日 18:43:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64044784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定