我是否需要删除我的java应用程序创建的tmp文件?

时间:2023-01-13 21:00:07

I output several temporary files in my application to tmp directories but was wondering if it is best practise that I delete them on close or should I expect the host OS to handle this for me?

我在我的应用程序中输出了几个临时文件到tmp目录,但是想知道我是否最好在关闭时删除它们,还是我希望主机操作系统为我处理这个?

I am pretty new to Java, I can handle the delete but want to keep the application as multi-OS and Linux friendly as possible. I have tried to minimise file deletion if I don't need to do it.

我是Java新手,我可以处理删除但希望将应用程序保持为多操作系统和Linux友好。如果我不需要这样做,我试图尽量减少文件删除。

This is the method I am using to output the tmp file:

这是我用来输出tmp文件的方法:

 try {
                java.io.InputStream iss = getClass().getResourceAsStream("/nullpdf.pdf");
                byte[] data = IOUtils.toByteArray(iss);
                iss.read(data);
                iss.close();
                String tempFile = "file";
                File temp = File.createTempFile(tempFile, ".pdf");
                FileOutputStream fos = new FileOutputStream(temp);
                fos.write(data);
                fos.flush();
                fos.close();
            nopathbrain = temp.getAbsolutePath();
            System.out.println(tempFile);
            System.out.println(nopathbrain);
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("TEMP FILE NOT CREATED - ERROR ");
            }

1 个解决方案

#1


5  

createTempFile() only creates a new file with a unique name, but does not mark it for deletion. Use deleteOnExit() on the created file to achieve that. Then, if the JVM shuts down properly, the temporary files should be deleted.

createTempFile()仅创建具有唯一名称的新文件,但不会将其标记为删除。在创建的文件上使用deleteOnExit()来实现这一点。然后,如果JVM正常关闭,则应删除临时文件。

edit: Sample for creating a 'true' temporary file in java:

edit:用于在java中创建“true”临时文件的示例:

File temp = File.createTempFile("temporary-", ".pdf");
temp.deleteOnExit();

This will create a file in the default temporary folder with a unique random name (temporary-{randomness}.pdf) and delete it when the JVM exits.

这将在默认临时文件夹中创建一个具有唯一随机名称(temporary- {randomness} .pdf)的文件,并在JVM退出时将其删除。

This should be sufficient for programs with a short to medium run time (e.g. scripts, simple GUI applications) that do sth. and then exit. If the program runs longer or indefinitely (server application, a monitoring client, ...) and the JVM won't exit, this method may clog the temporary folder with files. In such a situation the temporary files should be deleted by the application, as soon as they are not needed anymore (see delete() or Files helper class in JDK7).

对于具有中短运行时间的程序(例如脚本,简单的GUI应用程序)来说,这应该足够了。然后退出如果程序运行时间更长或无限期(服务器应用程序,监视客户端,...)并且JVM不会退出,则此方法可能会阻塞带有文件的临时文件夹。在这种情况下,应用程序应该删除临时文件,只要它们不再需要它们(请参阅JDK7中的delete()或Files helper类)。

As Java already abstracts away OS specific file system details, both approaches are as portable as Java. To ensure interoperability have a look at the new Path abstraction for file names in Java7.

由于Java已经抽象出特定于操作系统的文件系统细节,因此这两种方法都像Java一样可移植。为确保互操作性,请查看Java7中文件名的新路径抽象。

#1


5  

createTempFile() only creates a new file with a unique name, but does not mark it for deletion. Use deleteOnExit() on the created file to achieve that. Then, if the JVM shuts down properly, the temporary files should be deleted.

createTempFile()仅创建具有唯一名称的新文件,但不会将其标记为删除。在创建的文件上使用deleteOnExit()来实现这一点。然后,如果JVM正常关闭,则应删除临时文件。

edit: Sample for creating a 'true' temporary file in java:

edit:用于在java中创建“true”临时文件的示例:

File temp = File.createTempFile("temporary-", ".pdf");
temp.deleteOnExit();

This will create a file in the default temporary folder with a unique random name (temporary-{randomness}.pdf) and delete it when the JVM exits.

这将在默认临时文件夹中创建一个具有唯一随机名称(temporary- {randomness} .pdf)的文件,并在JVM退出时将其删除。

This should be sufficient for programs with a short to medium run time (e.g. scripts, simple GUI applications) that do sth. and then exit. If the program runs longer or indefinitely (server application, a monitoring client, ...) and the JVM won't exit, this method may clog the temporary folder with files. In such a situation the temporary files should be deleted by the application, as soon as they are not needed anymore (see delete() or Files helper class in JDK7).

对于具有中短运行时间的程序(例如脚本,简单的GUI应用程序)来说,这应该足够了。然后退出如果程序运行时间更长或无限期(服务器应用程序,监视客户端,...)并且JVM不会退出,则此方法可能会阻塞带有文件的临时文件夹。在这种情况下,应用程序应该删除临时文件,只要它们不再需要它们(请参阅JDK7中的delete()或Files helper类)。

As Java already abstracts away OS specific file system details, both approaches are as portable as Java. To ensure interoperability have a look at the new Path abstraction for file names in Java7.

由于Java已经抽象出特定于操作系统的文件系统细节,因此这两种方法都像Java一样可移植。为确保互操作性,请查看Java7中文件名的新路径抽象。