简体   繁体   中英

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

只需在字符串中添加“ \\ n”,然后将其写入文件即可:

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

Add this out.write("\\n".getBytes()); after out.write(b); it should give you the output you are looking for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM