Mohsen Ali
Mohsen Ali

Reputation: 701

Overwriting data using files

i created a file and when i run the program, the data is been written in to the file but when i run the program again the new data over write on old data. i need once the program is running and gathers data, these data are writable into the file in cascade next each other without overwriting on previous data in file.

this code running successful but when i run the program again the over writing happens which i don need that, i need to save previous data in side the file and write the new data next it and soon. after edit this code its looks like:

 public class MainActivity extends Activity {
        File file;
        File sdCard;
        FileOutputStream fos;
        OutputStreamWriter myOutWriter;
        String FileName = "Output3.txt";
        String eol;
        OutputStream fos1;
        FileWriter fw ;
        BufferedWriter writer;
        BufferedWriter bw ;
        PrintWriter out;
        EditText txtData;
        Button btnWriteSDFile;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                    sdCard =getExternalFilesDir(null);
                    file = new File(sdCard,FileName);
            txtData = (EditText) findViewById(R.id.editText1);
            btnWriteSDFile = (Button) findViewById(R.id.button1);
            btnWriteSDFile.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v)
                {
        try {
                fos = new FileOutputStream(file);
                myOutWriter =new OutputStreamWriter(fos);
                eol = System.getProperty("line.separator");
                writer = new BufferedWriter(myOutWriter);
                                    writer.append(txtData.getText() + eol);// write this text.
                                    writer.flush();
                                    fos.close();

                                    Toast.makeText(v.getContext(),"Done writing SD 'Output.txt'", Toast.LENGTH_SHORT).show();
                                    txtData.setText("");
                                } catch (Exception e) {
                                    // e.printStackTrace();
                                    Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                                finally{
                                    try {
                                        fos.close();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }
                        });
                }





please,can any one answer me?

Upvotes: 2

Views: 229

Answers (4)

QuokMoon
QuokMoon

Reputation: 4425

try {
                    File myFile = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
                   // this changed bye fos = new FileOutputStream(myFile) to

                    fos = new FileOutputStream(myFile,true);
                    //FileWriter writer = new FileWriter("yourfilename", true);

                    myOutWriter = new OutputStreamWriter(fos);
                    eol = System.getProperty("line.separator");
                    writer = new BufferedWriter(myOutWriter);
                    writer.append("\n");
                    writer.append(txtData.getText() + eol);// write this text.
                    writer.flush();
                    fos.close();

                    Toast.makeText(v.getContext(),
                            "Done writing SD 'Output.txt'", Toast.LENGTH_SHORT)
                            .show();
                    txtData.setText("");
                } catch (Exception e) {
                    // e.printStackTrace();
                    Toast.makeText(v.getContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

Enjoye i have tested it is work for me.

Upvotes: 0

Tarsem Singh
Tarsem Singh

Reputation: 14199

use

FileWriter writer = new FileWriter("yourfilename", true);

second parameter should be passed as true which will append data instead of overwriting

Upvotes: 1

WiduX
WiduX

Reputation: 13

Change this line:

fos = new FileOutputStream(file);

To:

fos = new FileOutputStream(file, true);

This opens the file so that data can be appended.

JavaDocs for FileOutputStream

Upvotes: 1

Srikanth Roopa
Srikanth Roopa

Reputation: 1790

Instead of using OutputStreamWriter use Filewriter FileWriter fileWritter = new FileWriter(file,true); when u pass true it appends the contents.try this

FileWriter myOutWriter = new FileWriter(file.getName(),true);
            eol = System.getProperty("line.separator");
            writer = new BufferedWriter(myOutWriter);
                                writer.write(txtData.getText() + eol);// write this text.
                                writer.close();
                                fos.close();

Upvotes: 1

Related Questions