Java 中的 Windows 临时文件
如何在 Windows 中创建一个使用 Java 设置属性 FILE_ATTRIBUTE_TEMPORARY
和 FILE_FLAG_DELETE_ON_CLOSE
的文件?
我确实希望我的文件只是内存中的文件。
准确地说:退出时删除机制并不满足我,因为我想避免出现某些数据留在磁盘上的情况,例如应用程序崩溃。
How to create a file in Windows that would have attributes FILE_ATTRIBUTE_TEMPORARY
and FILE_FLAG_DELETE_ON_CLOSE
set using Java?
I do want my file to be just in-memory file.
To precise: delete-on-exit mechanism does not satisfy me, because I want to avoid situation, when some data is left on disk in case of, for example, application crash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用这样的东西。但它不会在内存中,而是在应用程序退出时删除的临时文件。
Use something like this. It won't be in-memory though, but a temporary file that is deleted when the app exits.
为什么不只使用内存块,即数据结构?创建文件背后的动机是什么?如果您想要临时文件,则 临时文件 和 退出时删除 会有所帮助。
Why not just use a memory block i.e. datastructure ? What's the incentive behind creating a file ? If you want a scratch file then temp file and delete on exit will help.
即使设置了两个标志,您的文件也可能最终出现在文件系统中。如果系统缓存变得太小,则文件将写入磁盘,如果系统崩溃,则不会执行后处理清理。
不过,我喜欢你的想法,并且想知道为什么 Windows 上的 JVM 实现默认不使用这些标志。至少应该像这样实现deleteOnExit()作为后备。
Even with both the flags set your files might end up in the filesystem. If the system cache becomes too small, the file is written to the disk, and if the system crashes, no afterprocess cleanup is performed.
However, I like your idea and wonder why the JVM implementation on Windows doesn't use the flags by default. At least deleteOnExit() should be implemented like this as a fallback.
在 Windows 上将文件标记为临时文件并在关闭时删除并不能保证它不会写入文件系统。
使用 UNIX / Linux,您可以在 TmpFS 或 RamFS 文件系统中创建文件;即在 RAM 内存中存储文件的文件系统。 TmpFS 由虚拟内存支持,因此 RamFS 中的部分或全部文件可能最终位于交换磁盘上。 RamFS 不受虚拟内存支持,只能驻留在 RAM 中。
RamFS 和 TmpFS 的概述可以在 找到在这里。
但请注意,RamFS 内容有可能(至少在理论上)最终保存在光盘上。
如果系统进入休眠状态,则在系统断电之前,RAM 的全部内容将保存到光盘。
如果可以诱导内核崩溃并且启用了内核崩溃转储,则内核内存的内容(可能包括 RamFS)将被写入转储。
Marking a file as temporary and delete-on-close on Windows won't guarantee that it is not written to the file system.
With UNIX / Linux you could create the file in TmpFS or RamFS file system; i.e. a file system that stores files in RAM memory. TmpFS is backed by virtual memory, so some or all of a file in RamFS may end up on the swap disc. RamFS is not backed by virtual memory, and should only ever reside in RAM.
An overview of RamFS and TmpFS can be found here.
Note however that it is possible (at least in theory) for RamFS contents to end up on disc.
If the system is put into hibernate state, the entire contents of RAM is saved to disc before the system is powered down.
If the kernel can be induced to crash and kernel crash dumps are enabled, the contents of kernel memory (probably including the RamFS) will be written to the dump.
您正在寻找特定于 Windows 的解决方案,那么为什么不使用通过 Processbuilder 执行的 wndows 命令创建文件。
You are after a windows specific solution, so why not create files using wndows commands execed via Processbuilder.