嗨我一直在嘗試很多工作來檢索文件的內容。我的目的是將一些內容寫入文件,並在單擊按鈕時將它們作爲文本視圖返回。我已經寫了一些代碼,但不幸的是,它不是working.Can在進階寫入文件和檢索文件的內容
public class Writing_to_fileActivity extends Activity {
/** Called when the activity is first created. */
public EditText edit;
public Button btn, read;
public TextView tv;
public String str;
public FileOutputStream fOut;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(R.id.edit);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edit.getText().toString();
try {
File sdcard = Environment.getExternalStorageDirectory();
fOut= openFileOutput("Bharath.txt",
MODE_WORLD_WRITEABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(str);
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
read =(Button)findViewById(R.id.read);
read.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(fOut));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
}
TextView tv = (TextView) findViewById(R.id.tet);
tv.setText(text);
}
});
}
}
你究竟是什麼意思,不工作?你是否得到一個例外,或者它只是沒有做你期望的? – Kingamajick
是什麼問題,具體是...添加一些日誌。或在輸出結果.. – AAnkit
我沒有得到文本view.ie,整個代碼運行正常,但文本視圖不會出現後,我點擊按鈕 –