use*_*410 11 java filesystems uri path
我正在尝试创建一个FileSystem对象来保存ext2文件系统.我URI似乎无效,给我一个路径组件应该是'/'运行时错误.
我正在使用Windows并在Eclipse中使用我的项目,其中包含一个名为"fs"的子目录,用于保存文件系统映像.
我的代码......
URI uri = URI.create("file:/C:/Users/Rosetta/workspace/filesystemProject/fs/ext2");
/* uri holds the path to the ext2 file system itself */
try {
FileSystem ext2fs = FileSystems.newFileSystem(uri, null);
} catch (IOException ioe) {
/* ... code */
}
Run Code Online (Sandbox Code Playgroud)
我已经将文件系统作为File对象加载并使用该getURI方法来确保我URI与实际相同URI,并且确实如此.
如何加载文件系统?
编辑:
下面的堆栈跟踪
Exception in thread "main" java.lang.IllegalArgumentException: Path component should be '/'
at sun.nio.fs.WindowsFileSystemProvider.checkUri(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newFileSystem(Unknown Source)
at java.nio.file.FileSystems.newFileSystem(Unknown Source)
at java.nio.file.FileSystems.newFileSystem(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
WindowsFileSystemProvider检查URI的路径是否只是'/'.uri作为URI完全有效,问题是FileSystem的必需品.crashystar是正确的(我还不能发表评论)并且应该使用Path.如果你读了newFileSystem(Path,ClassLoader)的JavaDoc,你会看到ClassLoader可以保留为null,所以你只需要做
Path path = Paths.get("C:/Users/Rosetta/workspace/filesystemProject/fs/ext2");
FileSystem ext2fs = FileSystems.newFileSystem(path, null);
Run Code Online (Sandbox Code Playgroud)
通过将其保留为null,Java尝试查找已安装的提供程序(因此您无法期望使用自定义提供程序).如果它是自定义提供程序,则必须使用可以加载该提供程序的ClassLoader.如果提供程序在您的类路径上,那就足够了
getClass().getClassLoader()
Run Code Online (Sandbox Code Playgroud)
既然你说你只是希望操作系统这样做,那就把它保留为null.
为什么不使用 Path 对象?
newFileSystem(Path path, ClassLoader loader)
Constructs a new FileSystem to access the contents of a file as a file system.
Run Code Online (Sandbox Code Playgroud)
注意三个构造函数:
static FileSystem newFileSystem(Path path, ClassLoader loader)
Constructs a new FileSystem to access the contents of a file as a file system.
static FileSystem newFileSystem(URI uri, Map<String,?> env)
Constructs a new file system that is identified by a URI
static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader)
Constructs a new file system that is identified by a URI
Run Code Online (Sandbox Code Playgroud)
小智 5
这在 Windows 上对我有用。尚未在其他操作系统中测试过
private void openZip(File runFile) throws IOException {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
env.put("encoding", "UTF-8");
System.out.println(runFile.toURI());
Files.deleteIfExists(runFile.toPath());
zipfs = FileSystems.newFileSystem(URI.create("jar:" + runFile.toURI().toString()), env);
//zipfs = FileSystems.newFileSystem(runFile.toPath(), getClass().getClassLoader()); //-----does not work
//zipfs = FileSystems.newFileSystem(URI.create("jar:file:/c:/Users/Siraj/Documents/AAAExport4.zip"), env); //---works
}
Run Code Online (Sandbox Code Playgroud)