Java內存文件系統:Jimfs
Jimfs 是一個用于 Java 7+ 的內存中的文件系統,實現了 java.nio.file 抽象文件系統 API。Jimfs 支持幾乎所有 java.nio.file下的APIs。它支持:
- 創建,刪除,移動和復制文件與目錄
利用 FileChannel
或 SeekableByteChannel
,InputStream
,OutputStream
等讀寫文件.- Symbolic links.
- Hard links to regular files.
SecureDirectoryStream
, for operations relative to an open directory.- Glob and regex path filtering with
PathMatcher
. - Watching for changes to a directory with a
WatchService
. - File attributes. Built-in attribute views that can be supported include "basic", "owner", "posix", "unix", "dos", "acl" and "user". Do note, however, that not all attribute views provideuseful attributes. For example, while setting and reading POSIX file permissions is possible with the "posix" view, those permissions will not actually affect the behavior of the file system. </ul>
Jimfs 還支持創建文件系統,for example, use Windows-style paths and (to an extent) behavior. In general, however, file system behavior is modeled after UNIX and may not exactly match any particular real file system or platform.
Maven
<dependency> <groupId>com.google.jimfs</groupId> <artifactId>jimfs</artifactId> <version>1.0</version> </dependency>
基本用法
最簡單的用法是使用Jimfs來取得一個新的FileSystem
實例,從 Jimfs
類:
import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; ... // For a simple file system with Unix-style paths and behavior: FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); Path foo = fs.getPath("/foo"); Files.createDirectory(foo); Path hello = foo.resolve("hello.txt"); // /foo/hello.txt Files.write(hello, ImmutableList.of("hello world"), StandardCharsets.UTF_8);
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!