Search code examples
javaspringfile-ioinputstream

Why won't input stream find a file outside of the class folder


I want to want to use input stream with a file "NewFile.java", the code I have works fine if the file is situated in the same folder as the .class file that is performing the action. But once I move I get null reference.

I have tried using absolute and relative paths with and without a '/' at the start.

InputStream in = getClass()
                .getResourceAsStream("NewFile.java");

I would like to source the file when it is located in the root directory of the project.


Solution

  • Better use InputStream in= new FileInputStream(new File("path/to/yourfile"));

    The way you are using it now is as a resource which has to be in the class path.