我想使用 Google 的 JIMFS 来创建一个用于测试目的的虚拟文件系统。不过,我很难开始。
我看了这个教程:http ://www.hascode.com/2015/03/creating-in-memory-file-systems-with-googles-jimfs/
但是,当我创建文件系统时,它实际上是在现有文件系统中创建的,即我不能这样做:
Files.createDirectory("/virtualfolder");
因为我被拒绝访问。
我错过了什么吗?
目前,我的代码如下所示:
测试类:
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path vTargetFolder = fs.getPath("/Store/homes/linux/abc/virtual");
TestedClass test = new TestedClass(vTargetFolder.toAbsolutePath().toString());
某处的Java类:
targetPath = Paths.get(targetName);
Files.createDirectory(targetPath);
// etc., creating files and writing them to the target directory
但是,我创建了一个单独的类来测试 JIMFS,这里目录的创建没有失败,但是我不能像这样创建一个新文件:
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path data = fs.getPath("/virtual");
Path dir = Files.createDirectory(data);
Path file = Files.createFile(Paths.get(dir + "/abc.txt")); // throws NoSuchFileException
我究竟做错了什么?