public void onClick(View v) {
File sdPath = Environment.getExternalStorageDirectory();
File f = new File(sdPath, "data.txt");
switch (v.getId()) {
case R.id.btn_save:
FileOutputStream fos;
String saveinfo = et_info.getText().toString();
try {
fos = new FileOutputStream(f);
fos.write(saveinfo.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "数据保存成功", 0).show();
break;
case R.id.btn_read:
String content = "";
try {
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
fis.close();
// BufferedReader br = new BufferedReader(new InputStream(fis)));
// br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "保存的数据是:" + content, 0).show();
break;
default:
break;
}
}
慕田峪7331174
相关分类