1

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

  1. 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.
  2. 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"))
  3. 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

  1. Does it use forward or backward slashes in the file Path?
  2. Do you need to include file extension? Why not? Is it not part of the file name?
  3. 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.
  4. 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 get

    By 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.

Community
  • 1
  • 1
Joop
  • 3,706
  • 34
  • 55
  • Wait a minute; you say Windows but the logs you paste seem to be extracted from Android... – fge Apr 20 '15 at 21:15
  • That's correct. You're right, I should edith that. I am running my code with Android Studio on a mobile device. But the files are on a Windows computer before I put it there. Little confusing for me.. – Joop Apr 20 '15 at 21:22
  • 1
    A program running on an android device simply does not have access to the file system of a development machine. Data from files in the project can be included in the application package, but it will not end up as actual files on the device, but rather as read-only constructs such as assets or resources which must be obtained with special APIs. – Chris Stratton Apr 20 '15 at 21:28

1 Answers1

1

I guess you want to save something on Android, then try these options http://developer.android.com/guide/topics/data/data-storage.html. Windows or whatever platform you use for development has nothing to do with the location of your files in Android.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134