我开始使用谷歌jimfs,我无法理解如何从路径获取文件.在源代码中,我看到Path.toFile抛出UnsupportedOperationException.但是如何在没有文件的情况下使用它呢?例如,如果我的应用程序需要知道某个路径是文件夹还是文件.
DefaultFileSystemProvider
例如,如何设置要使用JimfsFileSystemProvider
?javadoc FileSystems.getDefault()
说我需要设置一个系统属性,但当我尝试这样做时,我得到一个NoSuchMethodException
:
System.setProperty("java.nio.file.spi.DefaultFileSystemProvider",
"com.google.common.jimfs.JimfsFileSystemProvider");
FileSystems.getDefault();
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
java.lang.Error: java.lang.NoSuchMethodException: com.google.common.jimfs.JimfsFileSystemProvider.<init>(java.nio.file.spi.FileSystemProvider)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:128)
....
Run Code Online (Sandbox Code Playgroud)
我是否需要设置其他内容或者这是jimfs中的错误?
我有文件相关的代码来测试我想在哪里为我无法读取的现有文件测试错误处理。
class SomeClass {
//...
public void load(Path path) {
if (!Files.isRegularFile(path)) {
return null;
}
//...
try {
// ...
catch (IOException e) {
// cleanup
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用jimfs将测试与真实文件系统隔离。
那么我将如何在jimfs上创建一个不可读的文件?
我已经尝试过Files.setAttribute
在所需的路径上分配posix权限和其他用户到该路径,在尝试读取或写入该路径时,似乎都忽略了这两个路径。
我开始使用 google jimfs 并执行 ls 我发现 FS 根目录中有工作目录。该文件夹的用途是什么?
如何使用 jimfs 设置文件的上次修改日期?我有。像这样:
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path rootPath = Files.createDirectories(fileSystem.getPath("root/path/to/directory"));
Path filePath = rootPath.resolve("test1.pdf");
Path anotherFilePath = rootPath.resolve("test2.pdf");
Run Code Online (Sandbox Code Playgroud)
创建完这些东西后,我创建了一个目录迭代器,如:
try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(rootPath, "*.pdf")) {
final Iterator<Path> pathIterator = dirStream.iterator();
}
Run Code Online (Sandbox Code Playgroud)
之后,我遍历文件并读取最后修改的文件,然后返回:
Path resolveLastModified(Iterator<Path> dirStreamIterator){
long lastModified = Long.MIN_VALUE;
File lastModifiedFile = null;
while (dirStreamIterator.hasNext()) {
File file = new File(dirStreamIterator.next().toString());
final long actualLastModified = file.lastModified();
if (actualLastModified > lastModified) {
lastModifiedFile = file;
lastModified = actualLastModified;
}
}
return lastModifiedFile.toPath();
}
Run Code Online (Sandbox Code Playgroud)
问题是文件“test1.pdf”和“test2.pdf”的lastModified都为“0”,所以我实际上无法真正测试行为,因为该方法总是返回目录中的第一个文件。我试着做:
File file = …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用JGIT和JIMFS使用类似的方法将一个小型 git 配置存储库克隆到内存中
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path gitPath = Files.createDirectories(fs.getPath("/git"));
Git.cloneRepository().setURI(...).setBranch(...).setDirectory(gitPath.toFile())
.setCredentialsProvider(...).call()
Run Code Online (Sandbox Code Playgroud)
但由于 JIMFS 使用路径Path API(因为它不使用默认的文件系统),而 JGIT 使用File API,因此 JIMFS 不实现 toFile() 调用:
@Override
public File toFile() {
// documented as unsupported for anything but the default file system
throw new UnsupportedOperationException();
}
Run Code Online (Sandbox Code Playgroud)
所以我得到的是这个UnsupportedOperationException
。有没有一种简单的方法可以让这个(或类似的)设置工作而无需求助于磁盘上的临时目录?
我想使用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());
Run Code Online (Sandbox Code Playgroud)
Java类在某处:
targetPath = Paths.get(targetName);
Files.createDirectory(targetPath);
// etc., creating files and writing them to the target directory
Run Code Online (Sandbox Code Playgroud)
但是,我创建了一个单独的类来测试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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?