-1

I'm bewildered about the results that I'm obtaining in the Logcat. I'm trying to read a file but I'm getting a FileNotFoundException. Right now I'm hardcoding the url where the file is located.

InputStream readMyFile(String fileName){
    File file = new File("C:\\Users\\Alvaro\\workspaceEclipse\\ProyectA\\file.xml");
    //File file = new File("C:" +File.separator +"Users" +File.separator +"Alvaro" +File.separator +"workspaceEclipse" +File.separator +"ProyectA" +File.separator +"file.xml");
    System.out.println("Working Directory = " +
              System.getProperty("user.dir"));
    System.out.println(file.exists());
    System.out.println(file.canRead());
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(file));
        if (in != null) {
            in.close();
        }
    }catch(Exception e){
        System.out.println("Error opening the file. \n" +e);
    }
    return in;
}

I can see that the file is not found because, somehow, a "/" is inserted at the beginning of the url string:

/C:\Users\Alvaro\workspaceEclipse\ProyectA\file.xml: open failed: ENOENT (No such file or directory)

The thing is, if I use the File.separator, the output is:

/C:/Users/Alvaro/workspaceEclipse/ProyectA/file.xml: open failed: ENOENT (No such file or directory)

Does anyone have an idea what may be happening?

Other information:

  • Working directory = /
  • file.exists() and file.canRead() obviously return false.
  • Windows 7 Home Premium.
  • Eclipse Version Indigo Service Release 2
  • Java version 1.6.0_37
ajduke
  • 4,991
  • 7
  • 36
  • 56
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62

1 Answers1

2

I think that the problem is that you are not really running "on Windows".

Rather, I suspect that you are running inside an Android emulation environment. Android is designed for Linux and I would expect expect the Android runtime libraries in the emulation environment to assume that the file system uses UNIX / Linux style pathnames.

The give-away is that you are seeing messages in logcat. Logcat is an Android-ism. It is not native to Java on Windows or Java on Linux.


The other thing to note is that what you are trying to do (accessing files in the Eclipse workspace) is not something that will work on a real Android device. Rather than figuring out how to use Windows pathnames to access your Eclipse workspace, you'd be better off figuring out how to get the files into the Android app itself, or the (emulated) Android file system.


If you want to confirm this diagnosis, take your code sample and put it into a plain Java application and compile and run it from the command prompt.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks, I think you mentioned the two key points. :) This way of getting a file was meant to be a first step before downloading it from a website, but yes, this "quick" way of getting a file was just wrong. – AlvaroSantisteban Apr 15 '13 at 09:35