try {
if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
File saveFile = new File(sdCardDir, "a.txt");
FileOutputStream outStream = new FileOutputStream(saveFile); outStream.write(txtView.getText().toString().getBytes());
outStream.close();
Toast.makeText(AndroidTestActivity.this, "成功", Toast.LENGTH_LONG).show(); }
} catch (FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
通过上面代码,我在SD卡中创建了一个文件,存入数据的时候为什么后一条数据会把前一条数据替换掉,该如何解决!谢谢
public void save(String name) {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
File saveFile = new File(sdCardDir.getCanonicalPath(), name
+ ".txt");
FileOutputStream outStream = new FileOutputStream(saveFile,
true);
outStream.write(tv.getText().toString().getBytes());
outStream.write("\r\n".getBytes());
outStream.close();
Toast.makeText(Android_zxingActivity.this, "文件存储成功",
Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
}