我正在編寫一個程序,需要能夠寫入文本文件並稍後再讀取它們。要閱讀它們,我將它們設置爲viewtext。然而,視圖文本從不顯示任何內容。我已經縮小了myReader.readLine()返回NULL,這意味着我不寫任何文件或更可能,我沒有正確讀取文件。爲什麼我的viewtext不顯示任何內容?
會很感激的任何幫助,這,謝謝:)
public class openTable extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opentable);
File file = new File(getFilesDir()+"/table.xml");
try {
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("Test\n");
fOut.close();
Toast.makeText(openTable.this,"File Written",Toast.LENGTH_SHORT).show();}
catch (Exception e) {
Toast.makeText(openTable.this,"File Not Written",Toast.LENGTH_SHORT).show();
}
Log.d("FILEDIR",getFilesDir().toString());
TextView displayFile = (TextView) findViewById(R.id.displayFile);
try {
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
if(myReader.readLine() != null) {
Log.d("TEST","YES");
} else{
Log.d("TEST","NO");
}
while ((aDataRow = myReader.readLine()) != null) {
Log.d("ADATAROW",aDataRow);
aBuffer += aDataRow + "\n";
Log.d("ABUFFER",aBuffer);
}
displayFile.setText(aBuffer);
myReader.close();
Toast.makeText(openTable.this,"File Read",Toast.LENGTH_SHORT).show();}
catch (Exception e) {
Toast.makeText(openTable.this,"File Not Read",Toast.LENGTH_SHORT).show();
}
}
}
您是否已將'WRITE_EXTERNAL_STORAGE'權限添加到您的'AndroidManifest.xml'文件中。如果沒有,這可以解釋你的問題,因爲你永遠無法寫入文本文件。 – Willis
LogCat是否輸出任何錯誤? – tambykojak