在Java中附加到CSV文件的最后一行 [英] Appending to the last line of CSV file in Java

查看:475
本文介绍了在Java中附加到CSV文件的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是我目前拥有的代码,但它会覆盖当时csv文件中的所有数据,而不是将其附加到最后。是否有捷径可寻?

The code below is what I currently have, however it overwrites any data in the csv file at that time, instead of appending it to the end. Is there an easy way to do this?

public void printCustomerList() throws IOException{
        FileWriter pw = new FileWriter("F:\\data.csv");
        Iterator s = customerIterator();
        if (s.hasNext()==false){
            System.out.println("Empty");
        }
        while(s.hasNext()){
            Customer current  = (Customer) s.next();
            System.out.println(current.toString()+"\n");
            pw.append(current.getName());
            pw.append(",");
            pw.append(current.getAddress());
            pw.append("\n");
        }
            pw.flush();
            pw.close();
    }


推荐答案

尝试打开这样的文件

FileWriter pw = new FileWriter("F:\\data.csv",true); 

传递 true 附加参数。

这篇关于在Java中附加到CSV文件的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆