I am trying to read a text file which is set in CLASSPATH system variable. Not a user variable.
I am trying to get input stream to the file as below:
Place the directory of file (D:\myDir
)in CLASSPATH and try below:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("/SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("//SomeTextFile.txt");
Place full path of file (D:\myDir\SomeTextFile.txt
)in CLASSPATH and try the same above 3 lines of code.
But unfortunately NONE of them are working and I am always getting null
into my InputStream in
.
getResourceAsStream()
– Aaron Digulla Sep 23 '09 at 7:25File.Separator
- because you're not asking for a file, you're asking for a resource. It's important to understand that the abstraction involved isn't the file system. – Jon Skeet Jun 14 '13 at 17:59Class.getResourceAsStream
is really a convenience method for callingClassLoader.getResourceAsStream
, but with the additional feature of "relative" resources. If you're specifying an absolute resource, then any call using the same classloader will do the same thing. – Jon Skeet Nov 5 '15 at 18:07