开发者

FileNotFoundException only in android app

开发者 https://www.devze.com 2023-04-11 09:03 出处:网络
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.

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));
    S开发者_StackOverflow社区tring 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


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

You can use for your external drive:

Environment.getExternalStorageDirectory() + /* file */;


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消