简体   繁体   English

如何使用FileOutputStream在Java中的csv文件中写入下一行

[英]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. 我在变量average_time中获得了一个结果,后来将其转换为字节以便将其写入csv文件中。 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) 我希望是15(换行符)31(换行符)48(换行符)

But it shows 153148 但显示153148

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

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

Add this out.write("\\n".getBytes()); 添加此out.write("\\n".getBytes()); after out.write(b); out.write(b); it should give you the output you are looking for 它应该给您您正在寻找的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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