I have text file, which I want to read from, but I don`t really know how to navigate from the class path to the path where the file is.
This is my project structure: Project structure photo(Intellij)
public class Main {
public static void main(String[] args) {
URL fileURL = Main.class.getResource("resources/coding_qual_input.txt");
if (fileURL != null) {
File file = new File(fileURL.getFile());
try {
BufferedReader in = new BufferedReader(new FileReader(file));
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
I don't really know, if put the file correctly, but right now the new FileReader(file) gets me null.
So, why can't the FileReader read my file?
As Main.class.getResource("resources/coding_qual_input.txt") results in file:/C:/Users/Jonas/IdeaProjects/Android%20ATC/pyramidDecoder/out/production/pyramidDecoder/resources/coding_qual_input.txt, I don't see where the problem is.
Should I navigate somehow down the classpath?
Thanks for the help!
ok, as I suspected, the space (%20) in the folder name seems to not be nice with URL, so I've recreated that behavior and got a
FileNotFoundException(using Java 1.8)Using
ClassLoader.getSystemResourceAsStreamall works fine, here's:or (but I really discourage it) use
String.replaceto substitute "%20" with " " and still useURLor (again, not the best way imho) use
URL.openStreamto directly open anInputStreamwithout useFile