Why does getResourceAsStream with an empty string return an empty InputStream?

I just came across some kind of peculiar behavior with getResourceAsInputStream that I was hoping someone could shed some light on.

By passing this method, the name of a nonexistent resource returns zero, as I expected. However, passing it an empty or space-filled string actually returns a valid InputStream with zero bytes in it. These appear to be empty or blank spaces; spaces like "\ t" or "\ n" will result in a null value.

Is this the intended behavior? If so, what is the 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
+5
source share
3 answers

getResourceAsStream ClassLoader URL- . , .class, FileURLConnection. getResourceAsStream getInpuStream(), , ByteArrayInputStream .

FileURLConnections , ...

+3

:

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();

, . :

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
+4

:

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") < - .

, \t . , , .

0

All Articles