Android save to file.txt appending
Asked Answered
E

4

5

I admittedly am still learning and would consider myself a novice (at best) regarding programming. I am having trouble with appending a file in android. Whenever I save, it will rewrite over the file, and I am having trouble understanding how to keep the file that is already there and only add a new line. Hoping for some clarity/advice. Here is how I am saving to the file (which rewrites the file each time I save).

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();
    }
}
Ekaterinodar answered 11/12, 2014 at 3:50 Comment(0)
E
8

Change this,

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

to,

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

This will append your new contents to the already existing file.

I Hope it helps!

Equip answered 11/12, 2014 at 4:7 Comment(1)
yep. So, MODE_APPEND was my issue. Thank you very muchEkaterinodar
C
8

Use this method, pass filename and the value to be added in the file

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) {
    }

}
Cyclometer answered 11/12, 2014 at 3:53 Comment(0)
E
8

Change this,

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

to,

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

This will append your new contents to the already existing file.

I Hope it helps!

Equip answered 11/12, 2014 at 4:7 Comment(1)
yep. So, MODE_APPEND was my issue. Thank you very muchEkaterinodar
H
1

To display the content of the saved file with the line breaks with a button click use:

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

To Delete content of existing file whist retaining the filename you can use:

    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();
        }
    }
     });
Hartz answered 16/9, 2018 at 8:52 Comment(0)
H
0

This worked for me. Takes content of a TextEdit called textTitle. Writes it to file called dataTitle. Then writes a new line with fOut.write("\n"). The next text entered into TextEdit is added to the file with a line break.

  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();
            }
        }
    });
Hartz answered 16/9, 2018 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.