0

How is it that I only get FileNotFoundException in an android application and when I read it from a normal Java application it finds the directory.

I am using the same code exactly.

{
        BufferedReader bufRdr;
         contacts = new ArrayList<Contacts>();


        File randomContactsFile = new File(("C://test//randomcontacts.txt"));
        try {
         bufRdr = new BufferedReader(new FileReader(randomContactsFile));
    String line = null;
         String[] a = new String[2];

            while ((line = bufRdr.readLine()) != null)
                    {
                        a = line.split(",");
                        Contacts c = new Contacts(a[0], a[1], a[1], a[1], a[2]);
                        contacts.add(c);
                    }

        } catch (FileNotFoundException e) {
            Log.d("file not found", "check");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This is the exception:

 10-05 08:04:37.151: WARN/System.err(334):
 java.io.FileNotFoundException: /C:/test/randomcontacts.txt (No such
 file or directory) 10-05 08:04:37.151: WARN/System.err(334):     at
 org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
 10-05 08:04:37.161: WARN/System.err(334):     at
 dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
 10-05 08:04:37.161: WARN/System.err(334):     at
 java.io.FileInputStream.<init>(FileInputStream.java:80) 10-05
 08:04:37.161: WARN/System.err(334):     at
 java.io.FileReader.<init>(FileReader.java:42) 10-05 08:04:37.161:
 WARN/System.err(334):     at
 android.test.randomcontacts.RandomContactsActivity.readRandomContacts(RandomContactsActivity.java:177)
 10-05 08:04:37.161: WARN/System.err(334):     at
 android.test.randomcontacts.RandomContactsActivity.onCreate(RandomContactsActivity.java:55)
 10-05 08:04:37.161: WARN/System.err(334):     at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 10-05 08:04:37.161: WARN/System.err(334):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 10-05 08:04:37.161: WARN/System.err(334):     at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 10-05 08:04:37.161: WARN/System.err(334):     at
 android.app.ActivityThread.access$1500(ActivityThread.java:117)

Thank you

2 Answers 2

5

There is probably no C: drice on your android phone.

You can use for your external drive:

Environment.getExternalStorageDirectory() + /* file */;
2
  • so it only looks for files in the android's directory? you can't use external drives?
    – michelle
    Oct 5, 2011 at 8:08
  • you could use external drives, but android would not recognize it as your "C" drive, you must search for it first.
    – Xavjer
    Oct 5, 2011 at 8:10
1

This is this in this part of your code:

File randomContactsFile = new File(("C://test//randomcontacts.txt"));

C://test//randomcontacts.txt is not in unix path format.

If your developing for a phone/tablet you should use something like

/mnt/sdcard/test/randomcontacts.txt

or better:

File rootPath=Environment.getExternalStorageDirectory();
File randomContactsFile = new File(rootPath.getPath()+"/test/randomcontacts.txt");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.