Android保存到file.txt追加

时间:2014-12-11 03:50:55

标签: android append filewriter

我当然还在学习,并认为自己是编程方面的新手(充其量)。我在android中附加文件时遇到问题。每当我保存,它将重写文件,我无法理解如何保留已存在的文件,只添加一个新行。希望有一些清晰/建议。这是我如何保存到文件(每次保存时重写文件)。

public void saveText(View view){

    try {
        //open file for writing
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", MODE_PRIVATE));

        //write information to file
        EditText text = (EditText)findViewById(R.id.editText1);
        String text2 = text.getText().toString();
        out.write(text2);
        out.write('\n');

        //close file

        out.close();
        Toast.makeText(this,"Text Saved",Toast.LENGTH_LONG).show();

    } catch (java.io.IOException e) {
        //if caught
        Toast.makeText(this, "Text Could not be added",Toast.LENGTH_LONG).show();
    }
}

4 个答案:

答案 0 :(得分:6)

改变这一点,

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", MODE_PRIVATE));

要,

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", Context.MODE_APPEND));

这会将您的新内容附加到现有文件中。

我希望它有所帮助!

答案 1 :(得分:4)

使用此方法,传递文件名和要添加到文件中的值

public void writeFile(String mValue) {

    try {
        String filename = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + mFileName;
        FileWriter fw = new FileWriter("ENTER_YOUR_FILENAME", true);
        fw.write(mValue + "\n\n");
        fw.close();
    } catch (IOException ioe) {
    }

}

答案 2 :(得分:1)

要显示带有按钮的换行符的已保存文件的内容,请单击使用:

     b2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    try {
                        FileInputStream fin = openFileInput(fileTitle);
                        int c;
                        String temp = "";

                        while ((c = fin.read()) != -1) {
                            temp = temp + Character.toString((char) c);
                        }
                        tv.setText(temp);




                        Toast.makeText(getBaseContext(), "file read", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                    }
                }
            });

要删除保留文件名的现有文件的内容,可以使用:

    deleteOrder.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        try {
            FileOutputStream fOut = openFileOutput(fileTitle,MODE_PRIVATE);
           // fOut.write(data.getBytes());
            dataTitle = "";
            fOut.write(data.getBytes());
            fOut.close();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     });

答案 3 :(得分:0)

这对我有用。接受称为textTitle的TextEdit的内容。将其写入名为dataTitle的文件。然后使用fOut.write(“ \ n”)写入新行。输入到TextEdit中的下一个文本将以换行符添加到文件中。

  try {
                FileOutputStream fOut = openFileOutput(fileTitle,MODE_APPEND);
                fOut.write(dataTitle.getBytes());
                fOut.write('\n');
                fOut.close();

                Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });