英文:
Why does reading a test resource with ':' in filename result in a NPE?
问题
我试图确定如何从具有特殊字符:
的资源中读取文件。我正在主要代码中遍历目录,该代码返回文件列表,我使用这些列表来顺利地处理文件。
在我的单元测试中,做一些简单的操作,如:
this.getClass().getResource("/filename:containing.character")
由于存在:
,它会返回java.lang.NullPointerException
,可能是因为它认为它是一个协议。有什么解决方法吗?我不能重命名文件,因为文件名编码了我想在这种情况下进行测试的一些信息。
英文:
I am trying to determine how to read a file from resources with the special character :
in it's name. I am traversing the directory in my main code which returns File listings that I use to ingest the file without issue.
In my unit test doing something as simple as:
this.getClass().getResource("/filename:containing.character")
returns a java.lang.NullPointerException
due to the presence of the :
, presumably because it thinks it is a protocol. What is the work around for this? I cannot rename the file as the filename encodes some information I am looking to test in this case.
答案1
得分: 3
特殊字符用于引用资源
引用自T.伯纳斯-李等人的IETF RFC 2396规范,可在此处访问:https://www.ietf.org/rfc/rfc2396.txt
2.2. 保留字符
许多URI包括由特定特殊字符组成或分隔的组件。这些字符被称为“保留字符”,因为它们在URI组件内的使用仅限于其保留的目的。如果URI组件的数据与保留的目的冲突,那么必须在形成URI之前对冲突的数据进行转义。
保留字符 = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
和
- URI语法组件
URI语法取决于方案。通常,绝对URI编写如下:
<scheme>:<scheme-specific-part>
结果
Java 的 URL
类的实现(8,11,14)符合IETF RFC 2396规范文档。因此,资源的引用不应包含任何冒号 :
符号。
这将导致在解析具有您的输入 filename:containing.character
时引发 MalformedURLException
(请参见URL的OpenJDK实现,第652行)。接下来,在URLClassPath(第1254行)中将出现 null
值,因为前一个 MalformedURLException
实例尚未抛出,被“转换”为对 this.getClass().getResource(..)
调用的“非成功”响应。
结论
- 避免在文件名中使用冒号(
:
)符号 或 - 在在Java中通过URL处理它们之前,将受影响的文件重命名为“资源”。
英文:
Special characters for referencing resources
Quoting from the IETF RFC 2396 specification by T. Berners-Lee et al., accessible from here: https://www.ietf.org/rfc/rfc2396.txt
> 2.2. Reserved Characters
> Many URI include components consisting of or delimited by, certain
> special characters. These characters are called "reserved", since
> their usage within the URI component is limited to their reserved
> purpose. If the data for a URI component would conflict with the
> reserved purpose, then the conflicting data must be escaped before
> forming the URI.
>
> reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
> "$" | ","
and
- URI Syntactic Components
> The URI syntax is dependent upon the scheme. In general, absolute
> URI are written as follows:
>
> <scheme>:<scheme-specific-part>
Consequence
Java's implementation of the URL
class (8, 11, 14) is compliant with the IETF RFC 2396 specification document. Therefore, references to resources should/must not contain any colon :
symbol.
This will result in a MalformedURLException
when parsed with your input filename:containing.character
(see OpenJDK implementation of URL, line 652). Next, this will result in a null
value in URLClassPath (line 1254) as the previous instance of MalformedURLException
is not thrown yet "converted" to a "non successful" response to this.getClass().getResource(..)
call.
Conclusion
- Avoid colon (
:
) symbols in your filenames OR - Rename affected files before processing them as "resources" via URLs in Java.
答案2
得分: 1
除了@MWiesner的回答之外。
如果我们已经有这样的文件,我们可以做什么?
您可以通过参数指定包含这些文件的文件夹。然后遍历子文件夹列表,通过简单的名称比较找到所需的文件。
英文:
In addition to @MWiesner answer.
What we can do about if we already have files like that?
You can specify by parameter the folder in which those files are contained. And then traverse the list of children and find the desired file by simple name comparison.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论