Search code examples
javaresourcesclassloader

How load a txt file using ClassLoader?


I have the following code:

 public class App {
   public static void main(String[] args) {
     System.out.println(App.class.getClassLoader().getResource("test.properties"));
     System.out.println(App.class.getClassLoader().getResource("test.txt"));
     System.out.println(App.class.getClassLoader().getResourceAsStream("test.properties"));
     System.out.println(App.class.getClassLoader().getResourceAsStream("test.txt"));
     System.out.println(App.class.getResourceAsStream("test.properties"));
     System.out.println(App.class.getResourceAsStream("test.txt"));
   }
 }

This code gives the following output:

file:/C:/../test.properties 
null 
java.io.BufferedInputStream@18e2b22 
null 
java.io.BufferedInputStream@1cb1c5fa 
null 

Why does ClassLoader refuses to load .txt file?

Updated:

I'm sure that both of my files in the classpath. Classpath is set to /src and both files are in this directory.


Solution

  • Most likely your txt file is not on the classpath