首页新闻找找看学习计划

Android文件存储问题!

0
悬赏园豆:20 [已解决问题] 解决于 2012-10-18 09:30

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卡中创建了一个文件,存入数据的时候为什么后一条数据会把前一条数据替换掉,该如何解决!谢谢

ii  乄筱跃____的主页 ii 乄筱跃____ | 初学一级 | 园豆:6
提问于:2012-09-10 11:42
< >
分享
最佳答案
0

 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;
  }
 }

ii 乄筱跃____ | 初学一级 |园豆:6 | 2012-10-18 09:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册