我尝试从文件中读取一些inf,所以我得到了readFromFile()
,但是我的应用程序开始读取它时就会崩溃。这是readFromFile()
:
private String readFromFile() {
String ret = "";
try {
Log.w(TAG, "Start read");
InputStream inputStream = openFileInput(filename);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
int hnum = 1;
while ( (receiveString = bufferedReader.readLine()) != null ) {
Log.d(TAG, "hnum = " + hnum);
stringBuilder.append(receiveString);
if (hnum == 1) {num = Integer.valueOf(stringBuilder.toString());}
if (hnum == 2) {sex = Integer.valueOf(stringBuilder.toString());}
else { ret = stringBuilder.toString();}
hnum = hnum + 1;
}
inputStream.close();
ret = stringBuilder.toString();
Log.i(TAG, "Ret = " + ret);
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File not found: " + e.toString());
Toast.makeText(this, "Profile file not found", Toast.LENGTH_SHORT).show();
num = 0;
writeToFile(data);
} catch (IOException e) {
Log.e(TAG, "Can not read file: " + e.toString());
Toast.makeText(this, "Can not read profile file", Toast.LENGTH_SHORT).show();
}
return ret;
}
和logcat:
W/Days﹕ Start read
D/Days﹕ hnum = 1
虚拟设备
App Days stopped
提前致谢, SergaRUS