错误:`error - java.lang.IllegalArgumentException:从类路径获取文件时,URI不是分层的 [英] Error: `error - java.lang.IllegalArgumentException: URI is not hierarchical while getting a file from a classpath

查看:1051
本文介绍了错误:`error - java.lang.IllegalArgumentException:从类路径获取文件时,URI不是分层的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含在类路径中的目录中的文件。它看起来像 pl / shenlon / io / gui / appData / file.txt 。现在,当我尝试将其转换为文件并使用此代码读取时:

I have a file contained within a directory in a classpath. It looks like this pl/shenlon/io/gui/appData/file.txt. Now, when I try to convert it to a File and read with this code:

File cityNamesFile = new File(ClassLoader.getSystemResource("pl/shenlon/io/gui/appData/list.txt").toURI());
Scanner cns = new Scanner(cityNamesFile);

我得到以下信息: -

I get the following:-

错误 - java.lang.IllegalArgumentException:URI不是分层的。

如何解决此问题?

推荐答案

如果您的调用类本身与文本文件位于同一个包中,只需使用:

If your calling class is itself in the same package as the text file, just use :

InputStream is = getClass().getResourceAsStream("list.txt");
Scanner cns = new Scanner(is);

这篇关于错误:`error - java.lang.IllegalArgumentException:从类路径获取文件时,URI不是分层的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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