- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我和我的同事正在争论 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);
我知道这有点不清楚,但它的一般要点是它创建了 Clob
的 AsciiOutputStream
(是的,它应该是一个 Clob
) 并将其写入从先前方法传递的 inStream
对象。
由于 File.delete();
方法,他们确信这不会写入文件目录,但我确实知道昨天那个位置有一个文件,并且此代码今天运行并在该确切位置写入了一个文件。因为,虽然实际的 file 被删除了,但是那个文件所在位置的 pointer 仍然在 outFile
中,而 的创建inStream
和 outFile
使 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 aFile
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 newFileDescriptor
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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!