Skip to content Skip to sidebar Skip to footer

Saving In A File Results In Many Instances Of Variables

I am using below function to save some data. The mydata is double list that user enter and dates_Strings is list (with String as datatype) which contain dates in string format. pub

Solution 1:

What you should see in the file is more something like

1.0->07/05/131.0->07/05/132.0->07/05/131.0->07/05/132.0->07/05/133.0->07/05/131.0->07/05/132.0->07/05/133.0->07/05/134.0->07/05/13

Thats because when you write myData to the file you always write the whole list, including the numbers you've written before. So the first save writes 1.0, the second save writes 1.0 and 2.0 and so on. You can fix this by instantiating your FileOutputStream with fos = new FileOutputStream(file,false);, then it won't append the new data to the file, but overwrite it. Or you could just write the newly saved number alone to the file with append mode. Depends on your use-case which one is better.

EDIT: What I meant by write the number alone would be something like this:

instead of

for (int i=0;i<mydata.size();i++){
    bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
}

You write

bw.write(thedata + "," + formattedDate + "\n");

Solution 2:

fos = new FileOutputStream(file,true);

you are instantiating FileOutputStream with append flag. If mydata is never cleared, its cotent will be write in the file everytime you call savefunc

Post a Comment for "Saving In A File Results In Many Instances Of Variables"