OutputStreamWriter不追加 [英] OutputStreamWriter does not append

查看:788
本文介绍了OutputStreamWriter不追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来的code和它的工作将数据保存到SD卡

Original code and its working saving the data to the SD Card

// Writing data to internal storage
btnSaveData.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
   if (isSDCardWritable()) {
      String dataToSave = etData.getText().toString();
         try {
         // SD Card Storage
         File sdCard = Environment.getExternalStorageDirectory();
         File directory = new File(sdCard.getAbsolutePath()+"/MyFiles");
         directory.mkdirs();
         File file = new File(directory, "text.txt");
         FileOutputStream fos = new FileOutputStream(file);
         OutputStreamWriter osw = new OutputStreamWriter(fos);

         // write the string to the file
         osw.write(dataToSave);
         osw.flush();
         osw.close();
         . . . 

然后,我改变了code追加一个新的值应根据我所需要的是:

And then I changed the code to append a new values as it should be according to what I need:

            osw.append(dataToSave);
            osw.flush();
            osw.close();

但问题是:它覆盖的文本文件,而不是追加。我错过了什么?感谢您的帮助

Problem is: It overwrites the text file instead of appending. What did I miss? Thanks for helping

推荐答案

构造的FileOutputStream(档案文件)始终覆盖文件。如果你要附加到文件,你必须使用更多通用的构造函数的FileOutputStream(文件档案,布尔追加)。如果设置参数'追加'到真正的文件不会被覆盖。

Constructor FileOutputStream( File file ) always overwrites file. If you want to append to file you have to use more generic constructor FileOutputStream( File file, boolean append ). If you set parameter 'append' to true file is not overwritten.

这篇关于OutputStreamWriter不追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆