我正在尝试创建一个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 */
}我已经将文件系统作为一个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)发布于 2014-12-10 09:14:54
WindowsFileSystemProvider检查URI的路径是否只有'/‘。URI作为uri是完全有效的,问题是文件系统的要求。crashystar是正确的(我还不能评论),应该使用路径。如果您阅读JavaDoc of newFileSystem(Path,ClassLoader),您将看到ClassLoader可以保留为null,所以只需执行
Path path = Paths.get("C:/Users/Rosetta/workspace/filesystemProject/fs/ext2");
FileSystem ext2fs = FileSystems.newFileSystem(path, null);通过将其保留为空,Java试图定位已安装的提供程序(因此您不能期望使用自定义提供程序)。如果它是自定义提供程序,则必须使用可以加载该提供程序的ClassLoader。如果提供者在您的类路径上,这就足够了。
getClass().getClassLoader()既然你说你只想让操作系统这么做,就让它为空。
发布于 2014-11-22 16:01:46
为什么不使用Path对象?
newFileSystem(Path path, ClassLoader loader)
Constructs a new FileSystem to access the contents of a file as a file system.注意三个构造函数:
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发布于 2017-05-08 13:36:51
这在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
}https://stackoverflow.com/questions/27078996
复制相似问题