2
对于单元测试,我想用VFS创建一个内存中的文件系统。用apache VFS创建虚拟文件系统
我当前的代码:
final String ROOTPATH = "ram://virtual";
FileSystemManager fsManager = VFS.getManager();
fsManager.createVirtualFileSystem(ROOTPATH);
FileObject testFile = fsManager.resolveFile(ROOTPATH + "/test.txt");
testFile.createFile();
FileObject testFile2 = fsManager.resolveFile(ROOTPATH + "/test2.txt");
testFile2.createFile();
FileObject testFile3 = fsManager.resolveFile(ROOTPATH + "/test3.txt");
testFile3.createFile();
FileObject testFile4 = fsManager.resolveFile(ROOTPATH + "/test4.txt");
testFile4.createFile();
FileObject folder = fsManager.resolveFile(ROOTPATH);
FileObject[] files = folder.getChildren();
for (FileObject file : files) {
System.out.println(file.getName());
}
我的问题:这是做正确的方法是什么?这个topica的例子很稀少。
我仍然得到日志消息:
Apr 14, 2015 11:08:17 AM org.apache.commons.vfs2.VfsLog info
INFORMATION: Using "/tmp/vfs_cache" as temporary files store.
我可以忽略这一点,因为我使用的RAM URI方案?我想这是因为我没有配置DefaultFileSystemManager。
感谢您的帮助和提示!
编辑:
现在与马绍尔memoryFileSystem:
我从他们的网站复制的示例代码。
这是我的@试验方法:
FileSystem fileSystem = this.rule.getFileSystem();
Path testDirectoryPath = Paths.get("test");
Files.createDirectories(testDirectoryPath);
Path p = fileSystem.getPath("test");
System.out.println(p.getFileName());
System.out.println(p.getFileSystem());
Path testfile = Paths.get("test/text2.txt");
Path test = Files.createFile(testfile);
Path f = fileSystem.getPath("test/text2.txt");
System.out.println(f.getFileName());
System.out.println(f.getFileSystem());
System.out.println(f.toAbsolutePath());
这是控制台输出:
test
MemoryFileSystem[VirtualTestFileSystem]
text2.txt
MemoryFileSystem[VirtualTestFileSystem]
/test/text2.txt
看起来还好,但是:这些文件和目录实际上得到我的硬盘驱动器上创建的,在项目文件夹。我想,这整个意思是为了避免这个......?我做错了什么,或者我只是不明白...?
如果你使用Java 7+,你应该考虑使用java.nio.file来代替;并且确实存在[相当不错的内存中实现](https://github.com/marschall/memoryfilesystem)。 – fge
感谢您的回复!有什么优势? – user3629892
很多;基本上,您只需使用标准的java.nio.file API来操作其中的条目,就像使用本地磁盘上的文件一样。 – fge