Search code examples
javafilecsvfileoutputstream

How to write to next line in a csv file in java using FileOutputStream


I have a result obtained in the variable average_time which I later converted to bytes in order to get it written in a csv file. The result is obtained three times as I have used a loop. I want the results to be printed one by one below in the same column. But the problem is that all the values are being written in the same cell. How can I solve this problem?

    try{    
        FileOutputStream out=new FileOutputStream("testout.csv");
        String str=Integer.toString(average_time);
        byte b[]=str.getBytes();
        out.write(b);
        }
        out.close();
         //System.out.println("success...");    
        }catch(Exception e){System.out.println(e);}    
  }    

I expect it to be 15 (newline) 31 (newline) 48 (newline)

But it shows 153148


Solution

  • Just add "\n" to str before write it to file:

    String str=Integer.toString(average_time) + "\n";