android中的filenotfoundexception

时间:2011-10-04 15:27:05

标签: java android filenotfoundexception

当我在main中运行这段代码作为普通的java应用程序时运行正常。但是当我尝试在其中一个活动中使用onCreate中的相同代码时,它表示文件未找到异常并且在logcat中不打印任何内容。我已经尝试了所有可能的方式,但不知道问题的原因。同样在logcat中,msg就像这个filenotfound异常:/D:/android/Suyesh.2DV106.Assignment3/southern_cities.txt(没有这样的文件或目录)。是因为前导斜线/ D:/ ..但我没有把它放在那里,当我尝试打印路径时它不包含/在D前面。但是当我打印absoulte路径它包含/ D 。问题出在这里?我还有我的清单文件如下。

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>

3 个答案:

答案 0 :(得分:1)

Android基于Linux,所以像“D:”这样的东西永远不会起作用。

如果文件在SD卡上,请尝试:

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

最后你的道路应该是这样的:
"/mnt/sdcard/android/Suyesh.2DV106.Assignment3/southern_cities.txt"

答案 1 :(得分:0)

如果D是您的外部存储目录(SD卡或闪存),请使用Enviroment.getExternalStorageDirectory()

答案 2 :(得分:0)

您是否尝试过使用:

File file = new File(TheCityMap.class.getResource(/* name */));
BufferedReader reader = new BufferedReader(new FileReader( file ));

或:

new BufferedReader(new InputStreamReader(FlowAp.class.getResourceAsStream("" /* name */)))