gpt4 book ai didi

java - File.delete() 是否删除 File 对象的指针?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:08 26 4
gpt4 key购买 nike

我和我的同事正在争论 File.delete() 方法在 Java 中的工作原理。

在我们的代码中:

File outFile = new File("/dir/name.ext");
if(outFile.exists())
outFile.delete();

FileInputStream inStream = new FileInputStream(outFile);

WriteFile.writeFile(inStream); // Writes the actual file

出于安全原因,我不能在此处包含 writeFile 的整个方法体,但在创建所需的数据库对象后,它会执行以下操作:

BufferedOutputStream out = null;

Object[] args = {"an_encrypted_data_clob_name_in_the_database"};
Class[] argTypes = {Class.forName("java.lang.String")};
Object result = WSCallHelper.jdbcCall(null, rs, "getCLOB", args, argTypes);
CLOB clob = (CLOB)result;
out = new BufferedOutputStream(clob.getAsciiOutputStream());

byte[] buffer = new byte[512];
int bytesRead = -1;

while((bytesRead = inStream.read(buffer)) > -1)
out.write(buffer, 0, bytesRead);

我知道这有点不清楚,但它的一般要点是它创建了 ClobAsciiOutputStream(是的,它应该是一个 Clob ) 并将其写入从先前方法传递的 inStream 对象。

由于 File.delete(); 方法,他们确信这不会写入文件目录,但我确实知道昨天那个位置有一个文件,并且此代码今天运行并在该确切位置写入了一个文件。因为,虽然实际的 file 被删除了,但是那个文件所在位置的 pointer 仍然在 outFile 中,而 的创建inStreamoutFile 使 inStream 指向该位置。

有什么理由相信在这种情况下不会写入此文件?理想情况下,我想要一些证据证明 delete() 方法删除了 File 对象指向 的文件,而不是指针本身。

最佳答案

java.io.File不是文件指针,也不包含文件指针。它是一个不可变的路径名。

An abstract representation of file and directory pathnames.

Instances of this class may or may not denote an actual file-system object such as a file or a directory.

Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

the source code for File ,我们可以看到它是 String 的包装器。

delete无法删除文件指针,因为没有文件指针。

Deletes the file or directory denoted by this abstract pathname.

打开文件的连接由 java.io.FileDescriptor 表示:

Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file […].

这是输入/输出流与文件系统交互的方式,而不是通过 File,例如 FileOutputStream(File)解释如下:

Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection.

If the file […] does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

我们可以观察到,例如,the constructor for FileOutputStream委托(delegate)给 File 的路径 String,检查它是否有效,然后丢弃 File:

public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
this.fd = new FileDescriptor();
fd.attach(this);
this.append = append;
open(name, append);
}

没有文档支持 java.io.File 表示文件指针的想法。 ; )

我们还知道,打开的文件句柄是一种必须在某个时候释放的资源,但是 File 没有提供这样做的方法;因此,File 不符合我们对文件指针应该是什么的概念。

关于java - File.delete() 是否删除 File 对象的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29437078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com