简体   繁体   English

在Linux上删除文件,但在Windows上删除

[英]File gets deleted on Linux but not on Windows

So I have this program in Java, where I make a file, write to it and save it. 因此,我有Java程序,可在其中创建文件,写入文件并保存。 But after the program finishes it's job, I want it to delete the file it created. 但是,程序完成工作后,我希望它删除其创建的文件。

Here is the code with which I make the file and delete it: 这是我用来制作文件并删除它的代码:

RandomAccessFile file = null;
file = new RandomAccessFile("myFile.zip", "rw");
file.write(buffer,0,read);
file.close();
File file = new File("myFile.zip");
file.delete();

It cannot be related to how Windows and Linux use their file paths ( \\ or /) as I don't really specify it other than showing it to be at the root of my project. 它与Windows和Linux如何使用其文件路径(\\或/)无关,因为除了将其显示在项目的根目录之外,我并未真正指定它。

So what might be the case in this situation? 那么在这种情况下会是什么情况?

Windows notices the open file handle and refuses to delete the open file. Windows注意到打开的文件句柄,并拒绝删除打开的文件。 That's a policy in Windows. 这是Windows中的一项政策。 Files which are open do not disappear. 打开的文件不会消失。 The process holding the open file handle can rely on that the file will stay. 持有打开文件句柄的进程可以依靠该文件将被保留。

Linux has a different policy. Linux有不同的策略。 There a file can be deleted from all directories (yes, it can be in more than one when it is hard linked), even if a process still has an open handle on it. 那里的文件可以从所有目录中删除(是的,当一个文件被硬链接时,它可以在多个目录中删除),即使一个进程上仍然有一个打开的句柄。 The file itself will then not be removed from the disk. 这样,文件本身将不会从磁盘上删除。 The process using the open handle can still process the file, make it grow, shrink it, write to it, read from it. 使用打开的句柄的过程仍然可以处理文件,使其增长,收缩,写入,读取。 But after the handle gets closed, the file gets removed automatically by the file system. 但是在关闭句柄之后,文件系统会自动删除该文件。

These different policies of the to OSes you are using are the reason for your observation. 您正在使用的OS的这些不同策略是您观察的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM