使用OutputStreamWriter追加一个字符串到一个现有的文件 [英] Appending a string to an existing file using OutputStreamWriter

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

问题描述

我不能够追加一个字符串使用 OutputStreamWriter 现有的文件。

据重写文件和旧数据已不存在:

 尝试{
    OutputStreamWriter outputStreamWriter =新OutputStreamWriter(openFileOutput(文件名,Context.MODE_PRIVATE));
    outputStreamWriter.append(消息);
    outputStreamWriter.close();
}
 

文件名是一个已经存在的文件。 的消息是一个字符串追加。

解决方案

使用MODE_APPEND而不是MODE_PRIVATE打开文件时

。 您的问题已回答已经好几次,喜欢这里: Android的附加文本文件

更新

不同的模式标志的打开文件时的目的(来源:的JavaDoc ):

  1. MODE_PRIVATE : 文件创建模式:默认模式,其中所创建的文件只能通过调用应用程序(或共享相同的用户ID的所有的应用程序)
  2. 访问。
  3. MODE_APPEND * :文件创建模式:与openFileOutput使用,如果文件已经存在,那么将数据写入到现有文件的末尾而不进行删除它。
  4. MODE_ENABLE_WRITE_AHEAD_LOGGING :数据库开放的标志:当设置,数据库的打开与默认启用的预写日志
  5. MODE_MULTIPROCESS :共享preference装载标志:当设置,磁盘上的文件都被检查,修改,即使共享preferences实例已加载在这个过程中。这种行为有时希望在情况下,应用程序有多个进程,都写相同的共享preferences文件。通常有更好的方式进程间的通信,虽然。这是传统的(但无证)行为和姜饼(Android 2.3的)前,目标定位等版本时,这个标志是隐含的。对于应用程序的目标定位SDK版本高于Android 2.3的,该标志必须显式设置如果需要的话。
  6. MODE_WORLD_READABLE 的:德precated
  7. MODE_WORLD_WRITEABLE 的:德precated

I'm not able to append a string to existing file using OutputStreamWriter.

It rewrites the file and old data no longer exists:

try {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("filename", Context.MODE_PRIVATE));
    outputStreamWriter.append(message);
    outputStreamWriter.close();
}

"filename" is a file that already exists. message is a string to append.

解决方案

Use MODE_APPEND instead of MODE_PRIVATE when opening the file. Your question has been answered already several times, like here: Android append text file

Update

The purpose of different mode flags when opening a file (source: JavaDocs):

  1. MODE_PRIVATE: File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
  2. MODE_APPEND*: File creation mode: for use with openFileOutput, if the file already exists then write data to the end of the existing file instead of erasing it.
  3. MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag: when set, the database is opened with write-ahead logging enabled by default.
  4. MODE_MULTIPROCESS: SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though. This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
  5. MODE_WORLD_READABLE: Deprecated
  6. MODE_WORLD_WRITEABLE: Deprecated

这篇关于使用OutputStreamWriter追加一个字符串到一个现有的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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