Problem Description
I need to give a proper file name path to in this example my FileReader(), but I always face problems in the process. I need a way to debug this myself and live a happy life.
StackOverflow provides a few posts about this subject but is in my opinion not making it very easy for me. It's is more or less describing the problem instead of solving it. See point 2. of what I tried where I back up this opinion.
What I tried
- First tried to find the Javadoc of FileReader which literally says nothing about the right path or how you can find it. It does not even hint about system differences.
- Search on stackOverflow and find relatively good answers:
Here is a well received answer of Stephen C which I find too hard to understand somehow, probably because he does not give examples and I am still left with many question. I also found a post about where java looks for files which of course is kind of a non specific question but at least gave me some sense of how I could debug it myself. Namely with
System.out.println(System.getProperty("user.dir"))
- Try out a lot of different paths, without success. I even ended up with an error, I have no idea what caused it but I literally only played with the paths which broke my R file in android studio. Removing the files and then adding them back solved the problem for me. Annoying and very weird.
Some basic questions I always end up facing
- Does it use forward or backward slashes in the file Path?
- Do you need to include file extension? Why not? Is it not part of the file name?
- Why are there system differences in the first place? I thought that Java was platform independent and all was included in the jar file of the program. This confuses me.
- How can I prevent searching for solutions and just know how to debug it myself. The only way I know it to find the path it looks in by default is with
System.out.println(System.getProperty("user.dir"))
which prints out/
for me which till leaves me with no clue. When I look it up on wikipedia I getBy default, the system root folder for Microsoft Windows is C:/Windows. When I give the full path name it still does not work C:/....
Some code in which I always try things
String str;
String testingFileLocation = "/app/src/main/res/dictionary/english";
System.out.println(System.getProperty("user.dir"));
try {
new FileReader(testingFileLocation);
} catch (IOException e) {
System.out.println("Wrong!");
}
Output
04-20 21:47:17.959 32152-32152/--- I/System.out﹕ /
04-20 21:47:17.959 2556-2556/--- I/System.out﹕ Wrong!
The stacktrace only gives this and does not bring me closer to any solution:
04-20 22:47:14.699 25934-25934/--- I/System.out﹕ java.io.FileNotFoundException: C:/Users/name/AndroidStudioProjects/projectPath: open failed: ENOENT (No such file or directory)
Maybe this looks more like a rant because of it's overly completeness, but this really frustrates me since there must be something I just don't understand. I hope you guys can help.