线程 "main" java.lang.IllegalArgumentException 中的异常:URI 不是分层的
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierar chical
我看到过类似的问题,但不确定如何解决。我试过将其更改为输入流
public List<String> mergeInputData(List<String> s){
List<String> mergedInputData = new ArrayList<String>();
for (String string : s) {
Enumeration<URL> en = getClass().getClassLoader().getResources(
string);
if (en.hasMoreElements()) {
URL metaInf = en.nextElement();
try (BufferedReader br = new BufferedReader(new FileReader(
new File(metaInf.toURI())))) {
String line = "";
while ((line = br.readLine()) != null) {
if (line.length() > 0)
mergedInputData.add(line.trim());
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return mergedInputData;
}
上面,字符串只包含文件名(比如egample.txt)例如我正在尝试读取egample.txt。
虽然它是从 eclipse 运行的。请建议我如何修复 it.I 已通过其他答案但不确定。
问题出在这里:
new File(metaInf.toURI())
如果您查看 File(URI)
构造函数的 javadoc,它说:
Creates a new File instance by converting the given file:
URI into an abstract pathname.
问题是 getResources()
枚举器提供的 URL/URI 通常是 而不是 file:
URL。如果您的代码是在 JAR 文件之外执行的,或者如果类路径上有其他 JAR 文件,那么您可以获得 jar:
个 URL。 File
构造函数无法处理它们...因为 URL 不使用主机文件系统中的路径名来命名。
在某些情况下,到此为止。但是,对于您的情况,您实际上并不需要使用 File
。相反,您应该可以这样做:
BufferedReader br = new BufferedReader(
new InputStreamReader(metaInf.openStream()));
在 Eclipse 中这是 运行 的原因是,在这种情况下,URL 将是 file:
URL。可直接从相应的 Eclipse 工作区目录访问资源。
我看到过类似的问题,但不确定如何解决。我试过将其更改为输入流
public List<String> mergeInputData(List<String> s){
List<String> mergedInputData = new ArrayList<String>();
for (String string : s) {
Enumeration<URL> en = getClass().getClassLoader().getResources(
string);
if (en.hasMoreElements()) {
URL metaInf = en.nextElement();
try (BufferedReader br = new BufferedReader(new FileReader(
new File(metaInf.toURI())))) {
String line = "";
while ((line = br.readLine()) != null) {
if (line.length() > 0)
mergedInputData.add(line.trim());
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return mergedInputData;
}
上面,字符串只包含文件名(比如egample.txt)例如我正在尝试读取egample.txt。
虽然它是从 eclipse 运行的。请建议我如何修复 it.I 已通过其他答案但不确定。
问题出在这里:
new File(metaInf.toURI())
如果您查看 File(URI)
构造函数的 javadoc,它说:
Creates a new File instance by converting the given
file:
URI into an abstract pathname.
问题是 getResources()
枚举器提供的 URL/URI 通常是 而不是 file:
URL。如果您的代码是在 JAR 文件之外执行的,或者如果类路径上有其他 JAR 文件,那么您可以获得 jar:
个 URL。 File
构造函数无法处理它们...因为 URL 不使用主机文件系统中的路径名来命名。
在某些情况下,到此为止。但是,对于您的情况,您实际上并不需要使用 File
。相反,您应该可以这样做:
BufferedReader br = new BufferedReader(
new InputStreamReader(metaInf.openStream()));
在 Eclipse 中这是 运行 的原因是,在这种情况下,URL 将是 file:
URL。可直接从相应的 Eclipse 工作区目录访问资源。