简体   繁体   English

为什么带有空字符串的getResourceAsStream返回一个空的InputStream?

[英]Why does getResourceAsStream with an empty string return an empty InputStream?

I just came across some peculiar behavior with getResourceAsInputStream that I was hoping someone could shed some light on. 我刚刚遇到了getResourceAsInputStream的一些特殊行为,我希望有人可以对此有所了解。

Passing this method the name of a nonexistent resource returns null, like I'd expect. 传递此方法,不存在的资源的名称将返回null,就像我期望的那样。 However, passing it an empty or space-filled string actually returns a valid InputStream with zero bytes in it. 但是,传递一个空的或空格填充的字符串实际上会返回一个有零字节的有效InputStream。 Only empty or space-filled strings seem to do this; 只有空的或空间填充的字符串似乎这样做; whitespace like "\\t" or "\\n" will result in a null. 像“\\ t”或“\\ n”这样的空格将导致null。

Is this intended behavior? 这是预期的行为吗? If so, what is it's purpose? 如果是这样,它的目的是什么?

this.class.getResourceAsStream("no_such_resource"); // returns null
this.class.getResourceAsStream("");                 // returns an InputStream
this.class.getResourceAsStream("    ");             // returns an InputStream
this.class.getResourceAsStream("\t");               // returns null

Try this code: 试试这段代码:

InputStream is = this.class.getResourceAsStream("");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while((line = br.readLine()) != null) System.out.println(line);
br.close();

this will print a list of classes that are located in the same directory of the current class. 这将打印位于当前类的同一目录中的类列表。 For example: 例如:

a.class
CallablePrintTask.class
java.policy.applet
RunnablePrintTask.class
ZoomableImageFrame.class
ZoomableImageFrame$FlagHolder.class
ZoomableImageFrame$ImageViewer.class
ZoomableImageFrame$LoadAction.class
ZoomableImageFrame$LoadAction$1.class
ZoomableImageFrame$ScaleAction.class

getResourceAsStream asks the ClassLoader to construct a URL for the path. getResourceAsStream要求ClassLoader构造路径的URL。 The path with an empty string or blanks at the end points to the directory of files where your classes .class file resides, so it constructs a FileURLConnection object. 末尾带有空字符串或空格的路径指向类.class文件所在的文件目录,因此它构造了一个FileURLConnection对象。 getResourceAsStream in turn asks that object to getInpuStream() and the implementation builds up a sorted directory listing in a string, converts it to bytes according to the default locale and gives you a ByteArrayInputStream on these bytes. getResourceAsStream反过来要求该对象为getInpuStream()并且该实现在字符串中构建一个已排序的目录列表,根据默认语言环境将其转换为字节,并在这些字节上为您提供ByteArrayInputStream

FileURLConnections behaviour is not very well documented, but if your search... FileURLConnections行为没有很好的记录,但如果您的搜索...

My guess is the following: 我的猜测如下:

this.class.getResourceAsStream("no_such_resource"); // returns null
this.class.getResourceAsStream("");                 // returns an InputStream
this.class.getResourceAsStream("    ");             // returns an InputStream
this.class.getResourceAsStream("\t");

getResourceAsStream("c:\\t") <-- is valid. getResourceAsStream("c:\\t") < - 有效。

It is coincidence that \\t is a tab character. 巧妙的是\\t是制表符。 Yet it is perfectly valid to have that as a path to look up. 然而,将它作为一种查找的途径是完全有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM