为什么读取文件名中包含’:’的测试资源会导致NPE?

huangapple go评论78阅读模式
英文:

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之前对冲突的数据进行转义。

  保留字符 = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
              "$" | ","

  1. URI语法组件

URI语法取决于方案。通常,绝对URI编写如下:

  <scheme>:<scheme-specific-part>

结果

JavaURL 类的实现(81114)符合IETF RFC 2396规范文档。因此,资源的引用不应包含任何冒号 : 符号。

这将导致在解析具有您的输入 filename:containing.character 时引发 MalformedURLException(请参见URL的OpenJDK实现,第652行)。接下来,在URLClassPath(第1254行)中将出现 null 值,因为前一个 MalformedURLException 实例尚未抛出,被“转换”为对 this.getClass().getResource(..) 调用的“非成功”响应。

结论

  1. 避免在文件名中使用冒号(:)符号
  2. 在在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

  1. 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

  1. Avoid colon (:) symbols in your filenames OR
  2. 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.

huangapple
  • 本文由 发表于 2020年8月7日 14:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定