问题
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>
回答1:
Android is based on linux so things like "D:" should never work.
If the file is on sd card try this:
String strFile = Environment.getExternalStorageDirectory() + "/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"
回答2:
Have you tried to use:
File file = new File(TheCityMap.class.getResource(/* name */));
BufferedReader reader = new BufferedReader(new FileReader( file ));
or:
new BufferedReader(new InputStreamReader(FlowAp.class.getResourceAsStream("" /* name */)))
回答3:
if D
is your external storage directory (sdcard or flash), use Enviroment.getExternalStorageDirectory()
来源:https://stackoverflow.com/questions/7650301/filenot-found-exception-in-android