Appending file results in overwrite (Java)

Go To StackoverFlow.com

4

So I am creating a CSV file and everytime an action occurs I would like to write the data to the file. The problem I am having is that it will overwrite the data when entering the second time. How do I add the data to the end of the file?

 public boolean save_to_csv(){

    //check if directory exists, if not create the folder
    File folder = new File(Environment.getExternalStorageDirectory() + "/HKA_CAL");
    //Environment.getExternalStorageDirectory() get the location of external storage
    boolean success = true;
    if(!folder.exists())
    {
       success = folder.mkdir();
    }         
    if (success) 
    { 
        //success is true if folder has successfully been created
        //now we can create/check if the file exists

        File stored_hka = new File(Environment.getExternalStorageDirectory()+"/HKA_CAL/Stored_values.csv");
        boolean file_existed=true;

        try{
            if(!stored_hka.exists()){
                stored_hka.createNewFile();
                file_existed=false;
            }
            FileOutputStream fOut = new FileOutputStream(stored_hka);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            if(!file_existed){
                //if the file did not exist we need to write the titles of the csv
                myOutWriter.append("Calibration Tracking\r\n");
                myOutWriter.append(",ZERO1,,Zero2,,cal1,,cal2,,CALIBRATION FACTORS\r\n");
                myOutWriter.append("Date,Stab,Read,Stab,Read,Stab,Read,Stab,Read,Unit S/N,F Zero,F Offset,F Factor\r\n");
            }
            myOutWriter.append("Date"
                    +","+get_step3_stab()+","+get_step3_read()
                    +","+get_step6_stab()+","+get_step6_read()
                    +","+get_step8_stab()+","+get_step8_read()
                    +","+get_step11_stab()+","+get_step11_read()
                    +","+get_sn_num()+","+get_f_zero()
                    +","+get_f_offset()+","+get_f_factor()+"\r\n"
                    );
            myOutWriter.close();
            fOut.close();
        }
        catch(Exception e){
            return false;
        }




        return true;
    }
    else 
    {
        return false;
    }



}
2012-04-03 20:25
by yawnobleix


8

Instead of doing

new FileOutputStream(stored_hka);

do

new FileOutputStream(stored_hka, true);

This will open the file stored_hka in append mode instead of overwriting the contents. See the javadoc for FileOutputStream(String name, boolean append) for more information

2012-04-03 20:28
by Jack Edmonds
That seems to work thanks a lo - yawnobleix 2012-04-03 20:39
@yawnobleix It also 'seems' to agree with the Javadoc - user207421 2012-04-03 23:45


3

When you construct your FileWriter or FileOutputStream there's a constructor argument which allows you to put it in append mode:

new FileOutputStream( "/path/to/file", true )
2012-04-03 20:27
by ulmangt


2

change

FileOutputStream fOut = new FileOutputStream(stored_hka);

to

FileOutputStream fOut = new FileOutputStream(stored_hka, true);
2012-04-03 20:29
by Angelo Fuchs


2

Use the FileOutputStream constructor that includes a boolean for appending

2012-04-03 20:30
by ControlAltDel


1

FileInputStream("valid path of file", true);

It will open a file in append mode. Boolean value is for whether you want to open a file in append mode or not.

2013-09-23 05:49
by Prasad
Ads