java - 调整 Java 7 中默认文件系统的行为

标签 java io filesystems java-7

Java 7 引入了一个用于编写自定义文件系统的出色 API。考虑一个我不想实现新文件系统的用例,我只想调整现有文件系统的行为。例如,翻转写入/读取的每一位。

在我看来,当前的 jdk 并没有为此提供适当的设施。 AbstractFileSystemProvider,WindowsFileSystemProvider 扩展的提供者是包私有(private)的,所以我不能重用它。我什至没有找到 Linux 的具体实现。

问题 #1:没有对当前文件系统进行有用的扩展抽象。

假设我只针对 Windows 进行扩展。 WindowsFileSystemProvider 是公共(public)的,所以我实际上可以轻松地覆盖 newByteChannel 并完成它。但是,唉!

问题 #2:WindowsFileSystem 是公开的,所以实际上我不得不编写一个全新的文件系统来引入一个新的 Provider。

我是不是遗漏了什么,或者这个功能是完全原始的,还没有准备好供应用程序编写者使用吗?

最佳答案

接触openjdk中的core-libs-dev后得到如下答复:

The service provider interface allows you to replace the default provider or interpose on it (see the FileSystems.getDefault docs for the details on how this is configured). When you interpose on the default provider then you have the opportunity to do your customization although it can be tricky to ensure that you get all the delegation right. As a starting point then look at the PassThroughFileSystem in jdk/test tree, this is a provider used by some of the tests and may be what you are looking for.

PassThroughFileSystem 是一个很好的引用实现,它演示了如何通过对默认提供程序的适当委托(delegate)来实现自定义提供程序。话虽如此,在我看来问题仍然存在,但至少我们有一个更好的起点。

关于java - 调整 Java 7 中默认文件系统的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19425836/

相关文章:

java - 单个 java 代码中的多个 JFrame

java - 从 .jar 加载文件时,URI 不是分层的

c++ - 为什么循环是无限的?

iOS7 到 iOS8 应用程序文件迁移

html - 样式表未应用于 html,因为它是 mime 类型

android - 如何在 chmod +x 不起作用的 android 操作系统上运行可执行二进制文件?

java - Spring-hibernate jpa 和 jboss - 将对象保存到第二个数据库

java - 如何阻止 Sun JDK1.6 内置 StAX 解析器解析 DTD 实体?

Java、EJB、所有方法的并发锁

c - 为什么在 C 中输入函数不能跟随输出函数,反之亦然?