count=0;
for ..{
if (count<4){
fout.print(i+",");count++;}
else {fout.print(i);count=0;}
}

try this