路径组件应为"/" [英] Path component should be '/'

查看:213
本文介绍了路径组件应为"/"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个FileSystem对象来保存ext2文件系统.我的URI似乎无效,给我一个 path组件应该是'/'运行时错误.

I'm trying to create a FileSystem object to hold an ext2 filesystem. My URI seems to be invalid, giving me a path component should be '/' run time error.

我正在使用Windows,并且在Eclipse中拥有我的项目,该项目的子目录名为"fs",用于保存文件系统映像.

I'm using Windows and have my project in Eclipse, with a subdirectory called "fs" that holds the filesystem image.

我的代码...

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相同,并且确实如此.

I have loaded the filesystem as a File object and used the getURI method to make sure my URI is the same as the actual URI, and it is.

如何加载文件系统?

下面的堆栈跟踪

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)

推荐答案

WindowsFileSystemProvider检查URI的路径是否仅为"/". uri作为URI是完全有效的,问题是FileSystem的必备条件. crashystar正确(我无法评论),应使用路径. 如果您阅读newFileSystem(Path,ClassLoader)的JavaDoc,您会看到ClassLoader可以保留为null,因此您只需要这样做

The WindowsFileSystemProvider's checks that the URI's path is only '/'. The uri is perfectly valid as URI, the problem is the FileSystem's requisites. crashystar has it right (I can't comment yet) and a Path should be used. If you read the JavaDoc of newFileSystem(Path, ClassLoader) you'll see the ClassLoader can be left at null, so you just need to do

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

通过将其保留为null,Java会尝试找到已安装的提供程序(因此,您不能期望使用自定义提供程序).如果它是一个自定义提供程序,则必须使用可以加载该提供程序的ClassLoader.如果提供者在您的类路径中,那就足够了

By leaving it at null Java tries to locate an installed provider (so you could not expect a custom provider to be used). If it were a custom provider you'd have to use a ClassLoader that can load that provider. If the provider is on your classpath, it'd be enough to do

getClass().getClassLoader()

由于您说过只希望操作系统执行此操作,因此请将其保留为空.

Since you say you just want the OS to do that, leave it at null.

这篇关于路径组件应为"/"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆