2014-11-22 85 views
6

我试图创建一个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) 
+1

您是否在路径'/ C:/ ...'前尝试过'file://这是协议',从而使'file :/// C:/ ...'? – 2014-11-22 15:25:11

+0

改变,给了我完全相同的错误:( – user155410 2014-11-22 15:27:48

+0

如何在URI的末尾添加'/'?如果ext2是挂载点,我希望它需要一个路径,所以'file:/ C :/ Users/Rosetta/workspace/filesystemproject/fs/ext2 /' – 2014-11-22 15:35:18

回答

4

堆栈跟踪为什么不使用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 
+1

我一开始试过,但我不知道'ClassLoader'是什么或是怎么样使用一个。我如何使用带有第一种方法的'ClassLoader'来获得'FileSystem'? – user155410 2014-11-22 16:04:24

4

的WindowsFileSystemProvider的检查,该URI的路径是唯一的 '/'。 URI作为URI完全有效,问题在于FileSystem的必要条件。 crashystar有它的权利(我不能评论),应该使用一个路径。 如果你读newFileSystem(路径,类加载器)的JavaDoc中您将看到的ClassLoader可以在空留,所以你只需要做

Path path = Paths.get("C:/Users/Rosetta/workspace/filesystemProject/fs/ext2"); 
FileSystem ext2fs = FileSystems.newFileSystem(path, null); 

通过在空留的Java尝试查找已安装的供应商(所以你不能指望自定义提供者被使用)。如果它是一个自定义提供程序,则必须使用可加载该提供程序的ClassLoader。如果供应商是在classpath中,这将会是足够的做

getClass().getClassLoader() 

既然你说你只想操作系统要做到这一点,把它在零。

1

你可以试试这个:

URI uri = URI.create("jar:file:/C:/Users/Rosetta/workspace/filesystemProject/fs/ext2"); 
+0

我发现这很有帮助,因为我的Uri被用来提取一个jar文件。我不知道当zipFileSystem在普通文件夹中打开。 – 2017-01-06 09:44:15

1

这为我工作在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 
} 
相关问题