FileNotFoundException异常仅在Android应用 [英] FileNotFoundException only in android app

查看:184
本文介绍了FileNotFoundException异常仅在Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是如何,我只在Android应用中得到FileNotFoundException异常,当我从一个普通的Java应用程序读取它找到的目录。

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.

我现在用同样的code准确。

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();
        }

这是个例外:

 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)

感谢您

推荐答案

这是这个在code的这一部分:

This is this in this part of your code:

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

C://test//randomcontacts.txt 不是UNIX路径格式

如果您开发一个手机/平板电脑,你应该使用类似

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

/mnt/sdcard/test/randomcontacts.txt

或更好的:

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

这篇关于FileNotFoundException异常仅在Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆