If it's already in the classpath, then just obtain it from the classpath. Don't fiddle with relative paths in java.io.File
. They are dependent on the current working directory over which you have totally no control from inside the Java code.
Assuming that ListStopWords.txt
is in the same package as FileLoader
class:
URL url = getClass().getResource("ListStopWords.txt");
File file = new File(url.getPath());
Or if all you're after is an InputStream
of it:
InputStream input = getClass().getResourceAsStream("ListStopWords.txt");
If the file is -as the package name hints- is actually a fullworthy properties file (containing key=value
lines) with just the "wrong" extension, then you could feed it immediately to the load()
method.
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("ListStopWords.txt"));
Note: when you're trying to access it from inside static
context, then use FileLoader.class
instead of getClass()
in above examples.