java - FileWriter false overwrites file from string A to Z BUT leaves only string Z; how do I keep string A-Z? -


so have following code within program. i'm trying print variables have declared in main of program csv file. want replace in csv file (the file exists before portion of code have replace there). now, tried (myfoo, false) happens program iterates through a-z , leaves z in csv file. want write a-z in csv file , not @ z.

my code:

     for(int t=0; t<superarray.size(); t++) {           string temp = superarray.get(t).character;          int temp2 = superarray.get(t).quantity;          int temp3 = superarray.get(t).width;          string temp4 = superarray.get(t).color;          int temp5 = superarray.get(t).height;           string eol = system.getproperty("line.separator");           string line=temp+","+temp2+","+temp3+","+temp4+","+temp5 + eol;            file myfoo = new file("letters.csv");          filewriter foowriter = new filewriter(myfoo, true);             foowriter.write(line);          foowriter.close(); 

the next thing thought of trying.. thought maybe (myfoo, true) , right before write file, clear out original content of csv file. append empty csv file.

         file myfoo = new file("letter_inventory.csv");          myfoo.createnewfile();          filewriter foowriter = new filewriter(myfoo, true); 

the logic sounds me, didn't work i'm here now. ideas? thanks!

you're re-opening , closing file each iteration of loop! over-write written file previous iterations of loop. solution is: don't that. open file once , before loop.

change

for(int t=0; t<superarray.size(); t++) {       // ....etc....      string line=temp+","+temp2+","+temp3+","+temp4+","+temp5 + eol;       file myfoo = new file("letters.csv");      filewriter foowriter = new filewriter(myfoo, true);         foowriter.write(line);      foowriter.close(); } 

to this

// file created *before* loop file myfoo = new file("letters.csv"); filewriter foowriter = new filewriter(myfoo, true);   for(int t=0; t<superarray.size(); t++) {       // .... etc ....      string line=temp+","+temp2+","+temp3+","+temp4+","+temp5 + eol;      foowriter.write(line); // line written inside loop  }  // should in block. foowriter.close(); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -