Reputation: 23360
I want to parse a text file called "hops.txt" which is located in a folder called "res"
inside the main project's folder.
When running the following code:
package parsing;
import java.io.InputStream;
public class ParseTest {
public static void main(String[] args) {
ParseTest pt = new ParseTest();
pt.foo();
}
public void foo()
{
InputStream is= this.getClass().getClassLoader().getResourceAsStream("hops.txt");
System.out.println(is);
is= this.getClass().getResourceAsStream("hops.txt");
System.out.println(is);
}
}
I recieve the following output:
null
null
What am I doing wrong? Thanks!
Upvotes: 0
Views: 463
Reputation: 23360
Solved the problem by deleting the folder res and putting all the files under a package with the name "res" under the "src" folder.
After that using the code:
InputStream is= this.getClass().getResourceAsStream("/res/hops.txt");
and it worked.
Upvotes: 1
Reputation: 28687
The path parameter of getResourceAsStream
is assumed to be relative to your project root. Because this text file is in a folder within your project, you must include the folder name in the resource path.
this.getClass().getResourceAsStream("/res/hops.txt");
Upvotes: 3