如何在内存中创建一个目录?伪文件系统/虚拟目录 [英] How to create a directory in memory? pseudo file system / virtual directory
问题描述
对于我的用例,我想有一个内存目录来存储一些文件很短的时间。实际上我在运行时将源代码编译为 .class
文件,并加载并执行它们。干净的方法是创建一个虚拟目录,让编译器在那里创建.class文件。当然我可以使用一个临时目录,但是在编译之前我必须清理它,我不知道我是否是唯一使用它的目录。
那么,在Java中是如何创建一个虚拟内存的目录?
在Java 7中,NIO API已经被扩展为提供一个API来允许您定义新的文件系统;请参阅
FileSystemProvider
。 Apache Commons VFS 是另一种选择,但它有一些可能会导致现有代码和(第三方)库问题的特征:
-
VFS中的文件和目录使用urls命名,而不是
File
对象。因此,使用File
进行文件操作的代码将不起作用。 FileInputStream ,
FileOutputStream
, FileReader
和 FileWriter
不会与VFS一起工作,原因很多。 For my usecase, I would like to have an in memory directory to store some files for a very short time. Actually I compile source code to .class
files at runtime, classload and execute them. The clean way would be to create a virtual directory and let the compiler create .class files there. Of course I could use a temp directory, but I have to clean it before compiling, I don't know if I'm the only one using it, etc.
So, is and how is it possible to create a virtual, in the meaning of in memory, directory in Java?
In Java 6 it is not really possible to do this kind of thing within a Java application. You need to rely on the OS platform to provide the the pseudo-filesystem.
In Java 7, the NIO APIs have been extended to provide an API that allows you to define new file systems; see FileSystemProvider
.
Apache Commons VFS is another option, but it has a couple of characteristics that may cause problems for existing code and (3rd-party) libraries:
Files and directories in VFS are named using urls, not
File
objects. So code that usesFile
for file manipulation won't work.FileInputStream
,FileOutputStream
,FileReader
andFileWriter
won't work with VFS for much the same reason.
这篇关于如何在内存中创建一个目录?伪文件系统/虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!