This discussion is archived
8 Replies Latest reply: Jul 19, 2010 10:43 AM by 843789 RSS

FileOutputStream FileNotFoundException error

843789 Newbie
Currently Being Moderated
Hi,

I am trying to write a byte[] to a png file, but I get a FileNotFoundException every time I try for some reason.
File file = new File("C:/test.png");
System.err.println(file.exists());
FileOutputStream fos = new FileOutputStream("C:/test.png");
The file.exists() returns false every time, even though the file is there, I can see it in explorer or in a command prompt, and there is no double extension or anything like that either. When I print my exception I get:

java.io.FileNotFoundException: /C:/test.png

Why is that extra slash being added? Is that what's causing the problem?

Thanks!

- Devon -
  • 1. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    Just a wild guess -- what operating system are you running this on?
    If it's not Windows: Windows-style file paths won't work on non-Windows systems.
  • 2. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    I've done it both on XP and Windows 7. I have also tried both "C:/test.png" as well as "C:\\test.png".
  • 3. Re: FileOutputStream FileNotFoundException error
    EJP Guru
    Currently Being Moderated
    Clearly you have a typo resulting from repeating the filename string. Try
    FileOutputStream fos = new FileOutputStream(file);
  • 4. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    Thanks for the response,

    No, that was not the issue, but you're correct, it was a typo when I posted my question. I have tried both ways, and besides, would it matter either way? If it doesn't work with the absolute path, shouldn't it not work with the File object as well?

    If it helps, when I do:
    System.err.println(file.getCanonicalPath());
    I get the output:
    /c:/test.png
    And also if it is important, this is for an Android application. Will that change the way the file paths work, even if I'm on a Windows machine?

    Thanks again.
  • 5. Re: FileOutputStream FileNotFoundException error
    EJP Guru
    Currently Being Moderated
    I don't understand the question. What happened when you tried what I suggested?
  • 6. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    Sorry for the confusion ejp.

    When I tried what you suggested, ie.
    File file = new File("c:\\test.png");
    System.err.println(file.toString());
    System.err.println(file.getCanonicalPath());
    System.err.println(file.exists());
    FileOutputStream fos = new FileOutputStream(file);
    I get the output:
    c:\test.png
    /c:/test.png
    false
    and the stack trace is
    java.io.FileNotFoundException: /c:\test.png
        at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
        at com.testAndroidApp.TestAndroidApp.initializeServerConnection(TestAndroidApp.java:202)
        at com.testAndroidApp.TestAndroidApp.onClick(TestAndroidApp.java:278)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4363)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        at dalvik.system.NativeStart.main(Native Method)
    I have tried both with:
    File file = new File("c:\\test.png");
    and
    File file = new File("c:/test.png");
    Hopefully this helps, thanks again!

    Edited by: daveldhu on Jul 19, 2010 8:41 AM
  • 7. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    The stack trace implies Android and your are dealing with windows-ish path names. Are your sure this is okay?
  • 8. Re: FileOutputStream FileNotFoundException error
    843789 Newbie
    Currently Being Moderated
    Silly me, yes, it's not running the program locally, so I can't use local or, for that matter, windows file paths.

    If anyone else makes this dumb mistake, see the Android documentation on data storage here: http://developer.android.com/guide/topics/data/data-storage.html

    Basically, use getCacheDir() to get a File representing the path for internal storage, then save any files in to that path. Mine was:
    /data/data/<package name>/cache
    Hope this will help someone who does the same thing.

    Thanks everyone for your help.