DELETE_ON_CLOSE的用处 [英] Usefulness of DELETE_ON_CLOSE

查看:407
本文介绍了DELETE_ON_CLOSE的用处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

互联网上有很多例子显示如何使用StandardOpenOption.DELETE_ON_CLOSE,例如:

There are many examples on the internet showing how to use StandardOpenOption.DELETE_ON_CLOSE, such as this:

Files.write(myTempFile, ..., StandardOpenOption.DELETE_ON_CLOSE);

其他示例同样使用 Files.newOutputStream(...,StandardOpenOption.DELETE_ON_CLOSE )

我怀疑所有这些例子都可能存在缺陷。写一个文件的目的是你要在某个时候读回来;否则,为什么还要写呢?但是,在您有机会阅读文件之前,DELETE_ON_CLOSE是否会导致文件被删除?

I suspect all of these examples are probably flawed. The purpose of writing a file is that you're going to read it back at some point; otherwise, why bother writing it? But wouldn't DELETE_ON_CLOSE cause the file to be deleted before you have a chance to read it?

如果您创建工作文件(至处理大量数据太大而无法保留在内存中)那么你不会使用 RandomAccessFile 来实现读写访问吗?但是,就我所见, RandomAccessFile 并没有给出指定DELETE_ON_CLOSE的选项。

If you create a work file (to work with large amounts of data that are too large to keep in memory) then wouldn't you use RandomAccessFile instead, which allows both read and write access? However, RandomAccessFile doesn't give you the option to specify DELETE_ON_CLOSE, as far as I can see.

所以有人可以告诉我DELETE_ON_CLOSE 实际有用吗?

So can someone show me how DELETE_ON_CLOSE is actually useful?

推荐答案

这里有两种可能的方式使用:

Here are two possible ways it can be used:

此方法返回 SeekableByteChannel 适用于读写,可以修改当前位置。

This method returns a SeekableByteChannel suitable for both reading and writing, in which the current position can be modified.

对于某些数据需要存储在内存中以进行读/写访问而且在应用程序关闭后不需要保留的情况看起来非常有用。

Seems quite useful for situations where some data needs to be stored out of memory for read/write access and doesn't need to be persisted after the application closes.

使用任意文本文件的示例:

An example using an arbitrary text file:

Path p = Paths.get("C:\\test", "foo.txt");
System.out.println(Files.exists(p));
try {
    Files.createFile(p);
    System.out.println(Files.exists(p));
    try (BufferedWriter out = Files.newBufferedWriter(p, Charset.defaultCharset(), StandardOpenOption.DELETE_ON_CLOSE)) {
        out.append("Hello, World!");
        out.flush();
        try (BufferedReader in = Files.newBufferedReader(p, Charset.defaultCharset())) {
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
} catch (IOException ex) {
    ex.printStackTrace();
}
System.out.println(Files.exists(p));

此输出(按预期):

false
true
Hello, World!
false

这个例子显然是微不足道的,但我想有很多情况都是这样的一种方法可能派上用场。

This example is obviously trivial, but I imagine there are plenty of situations where such an approach may come in handy.

但是,我仍然认为旧的 File.deleteOnExit 方法可能更适合您也不需要在文件的任何读取操作期间保持输出流保持打开状态。

However, I still believe the old File.deleteOnExit method may be preferable as you won't need to keep the output stream open for the duration of any read operations on the file, too.

这篇关于DELETE_ON_CLOSE的用处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆