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
.
Somehow the best answer doesn't work for me. I need to use a slightly different code instead.
I hope this help those who encounters the same issue.
Use
org.apache.commons.io.FileUtils.readFileToString(new File("src/test/resources/sample-data/fileName.txt"));
This is how I read all lines of a text file on my classpath, using Java 7 NIO:
NB this is an example of how it can be done. You'll have to make improvements as necessary. This example will only work if the file is actually present on your classpath, otherwise a NullPointerException will be thrown when getResource() returns null and .toURI() is invoked on it.
Also, since Java 7, one convenient way of specifying character sets is to use the constants defined in
java.nio.charset.StandardCharsets
(these are, according to their javadocs, "guaranteed to be available on every implementation of the Java platform.").Hence, if you know the encoding of the file to be UTF-8, then specify explicitly the charset
StandardCharsets.UTF_8
To actually read the contents of the file, I like using Commons IO + Spring Core. Assuming Java 8:
Alternatively:
If you use Guava:
we can get URL from CLASSPATH:
or InputStream:
You say "I am trying to read a text file which is set in CLASSPATH system variable." My guess this is on Windows and you are using this ugly dialog to edit the "System Variables".
Now you run your Java program in the console. And that doesn't work: The console gets a copy of the values of the system variables once when it is started. This means any change in the dialog afterwards doesn't have any effect.
There are these solutions:
Start a new console after every change
Use
set CLASSPATH=...
in the console to set the copy of the variable in the console and when your code works, paste the last value into the variable dialog.Put the call to Java into
.BAT
file and double click it. This will create a new console every time (thus copying the current value of the system variable).BEWARE: If you also have a User variable
CLASSPATH
then it will shadow your system variable. That is why it is usually better to put the call to your Java program into a.BAT
file and set the classpath in there (usingset CLASSPATH=
) rather than relying on a global system or user variable.This also makes sure that you can have more than one Java program working on your computer because they are bound to have different classpaths.