android中的filenot found异常 [英] filenot found exception in android

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

问题描述

当我将这段代码作为主要的Java应用程序运行时,它运行正常.但是,当我尝试在活动之一中的onCreate中使用相同的代码时,它说文件未找到异常,并且在logcat中不打印任何内容.我已经尝试了所有可能的方法,但不知道是什么原因引起的.同样在logcat中,msg类似于以下filenotfound异常:/D:/android/Suyesh.2DV106.Assignment3/southern_cities.txt(无此类文件或目录).是因为前导斜杠/D:/..但是我没有把它放在那儿,当我尝试打印路径时,它不包含该/在D前面.但是当我打印绝对路径时,它包含了/D.这里有什么问题?我也有清单文件如下.

when i run this piece of code as normal java application in main it runs fine. but when i try to use the same code in onCreate in one of the activity it says file not found exception and prints nothing in logcat. i have tried every possible way but dont know whts the cause of the problem. Also in logcat the msg is like this filenotfound exception: /D:/android/Suyesh.2DV106.Assignment3/southern_cities.txt (no such file or directory) . is it because of the leading forward slash /D:/.. but i didnt put it there and when i try to print the path it doesnt contain that / in front of D. But when i print the absoulte path it contains that /D. Whats the problem here? I have also my manifest file as below.

public class TheCityMap extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    try {
        String strFile = Environment.getExternalStorageDirectory() +"/android/Suyesh.2DV106.Assignment3/southern_cities.txt";
        BufferedReader br = new BufferedReader(new FileReader(strFile));
        String strLine = null;
        StringTokenizer st = null;
        int lineNumber = 0, tokenNumber = 0;
        while( (strLine = br.readLine()) != null){
            lineNumber++;
            st = new StringTokenizer(strLine, ",");
            while(st.hasMoreTokens()){
                tokenNumber++;
                System.out.println("Line # " + lineNumber +", Token # " + tokenNumber+ ", Token : "+ st.nextToken());
            }
            tokenNumber = 0; 

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
        }    

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="assignment3.demos"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainListActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="TheCityMap"></activity>
    <uses-library android:name="com.google.android.maps"/>
    <uses-permission android:name="android.permission.INTERNET" > </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" > </uses-permission>
</application>

推荐答案

Android是基于linux的,因此"D:"之类的东西将永远无法工作.

Android is based on linux so things like "D:" should never work.

如果文件位于sd卡上,请尝试以下操作:

If the file is on sd card try this:

String strFile = Environment.getExternalStorageDirectory() + "/android/Suyesh.2DV106.Assignment3/southern_cities.txt";

最后,您的路径应类似于:
"/mnt/sdcard/android/Suyesh.2DV106.Assignment3/southern_cities.txt"

At the end your path should be something like:
"/mnt/sdcard/android/Suyesh.2DV106.Assignment3/southern_cities.txt"

这篇关于android中的filenot found异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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