【问题标题】:Android write text in file Sd cardAndroid在文件SD卡中写入文本
【发布时间】:2014-06-05 11:28:54
【问题描述】:

我尝试在文件中写文本。我写了代码,女巫可以写文本,但如果我再次使用我的代码,文本会在文件中重写。例如,如果我第一次写“Hello android”然后“ Sir”,结果只有“Sir”。我想要“Hello android Sir”

your_file = new File("/sdcard/facebookUser");

                try {
                    Writer writer = new OutputStreamWriter(new FileOutputStream(
                            your_file), "UTF-8");
                    writer.write(facebook_user_name + ",");
                    writer.write(facebook_id);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (writer != null) {
                        try {
                            writer.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

如何编写代码以第二次在此文件中保存另一个文本?

【问题讨论】:

  • 你正在使用 .write - 尝试 .append(string) 代替
  • 你有什么解决方案?
  • 使用 StringBuilder 来处理

标签: android android-sdcard


【解决方案1】:
new FileOutputStream(your_file, true)

public FileOutputStream(String name, boolean append) throws FileNotFoundException

append - 如果为真,那么字节将被写入文件的末尾而不是开头

【讨论】:

    【解决方案2】:

    代替:

    writer = new OutputStreamWriter(new FileOutputStream(
                                your_file), "UTF-8"); 
    

    用途:

    writer = new OutputStreamWriter(new FileOutputStream(
                                your_file, true), "UTF-8");  
    

    这会将FileOutputStream 设置为附加模式。

    java.io.FileOutputStream.FileOutputStream(文件文件,布尔附加) 抛出 FileNotFoundException

    构造一个写入文件的新 FileOutputStream。如果追加是 true 并且文件已经存在,它将被附加到;否则它 将被截断。如果文件不存在,将创建该文件。

    【讨论】:

    • 我改变了这个来源,但现在我想在这个文件中添加一些文本和另一个活动。我在另一个活动中穿了这个代码 Registration.writer.append("Hello"); Registration.writer.close();
    • 您是否首先检查了您问题的这一部分:“.. 第二次在此文件中保存另一个文本的代码?” .从一项活动...如果您将内容写了两次..它是否附加在同一个文件中?然后那部分就固定了。。转移到另一个活动,其他的东西可能是个问题。。
    【解决方案3】:
    try {
            int n = 0;
            String Name = "file";
            File myFile = new File("/sdcard/test/");
            if (!myFile.exists()) {
                boolean b = myFile.mkdirs(); }
            myFile = new File("/sdcard/test/"+Name+".txt");
            while (myFile.exists())
            {
                myFile = new File("/sdcard/test/"+Name+n+".txt");  
                n++;
            }
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            String str = "Your text to write";
            myOutWriter.append(str);
            myOutWriter.close();
            fOut.close();
    } catch (Exception e) {}
    

    不要忘记许可:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    【讨论】:

    • 第一次写没问题,所以不是权限或其余代码:)
    • 好的,你应该读取旧文件(获取字符串),然后是 OldString+NewString 并覆盖旧文件
    • 没关系,如果文件已经存在,会追加新的文本。即便如此,从逻辑上讲,'oldstring+newstring' 似乎并不是最佳选择......只是说
    【解决方案4】:

    Vladimir Kulyk 和 user2450263 是正确的!您必须以附加模式写入文件。试试this链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多