Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to really read text file from classpath in Java

Tags:

java

classpath

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.

like image 920
Chaitanya MSV Avatar asked Sep 23 '09 06:09

Chaitanya MSV


People also ask

How do I read a file from SRC main resources?

Using Java getResourceAsStream() This is an example of using getResourceAsStream method to read a file from src/main/resources directory. First, we are using the getResourceAsStream method to create an instance of InputStream. Next, we create an instance of InputStreamReader for the input stream.

How do I load resources from classpath?

We can either load the file(present in resources folder) as inputstream or URL format and then perform operations on them. So basically two methods named: getResource() and getResourceAsStream() are used to load the resources from the classpath. These methods generally return the URL's and input streams respectively.

How do you load a file in Java?

Example 1: Java Program to Load a Text File as InputStream txt. Here, we used the FileInputStream class to load the input. txt file as input stream. We then used the read() method to read all the data from the file.


1 Answers

With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of:

// From ClassLoader, all paths are "absolute" already - there's no context // from which they could be relative. Therefore you don't need a leading slash. InputStream in = this.getClass().getClassLoader()                                 .getResourceAsStream("SomeTextFile.txt"); // From Class, the path is relative to the package of the class unless // you include a leading slash, so if you don't want to use the current // package, include a slash like this: InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt"); 

If those aren't working, that suggests something else is wrong.

So for example, take this code:

package dummy;  import java.io.*;  public class Test {     public static void main(String[] args)     {         InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");         System.out.println(stream != null);         stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");         System.out.println(stream != null);     } } 

And this directory structure:

code     dummy           Test.class txt     SomeTextFile.txt 

And then (using the Unix path separator as I'm on a Linux box):

java -classpath code:txt dummy.Test 

Results:

true true 
like image 132
Jon Skeet Avatar answered Sep 19 '22 11:09

Jon Skeet