Java将新列追加到csv文件

Java将新列追加到csv文件,第1张

Java将新列追加到csv文件

您将必须逐行读取文件,然后将新列插入每一行。这是使用BufferedReader和BufferedWriter的解决方案

public void addColumn(String path,String fileName) throws IOException{    BufferedReader br=null;    BufferedWriter bw=null;    final String lineSep=System.getProperty("line.separator");    try {        File file = new File(path, fileName);        File file2 = new File(path, fileName+".1");//so the         //names don't conflict or just use different folders        br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) ;        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));        String line = null;         int i=0;        for ( line = br.readLine(); line != null; line = br.readLine(),i++)        { String addedColumn = String.valueOf(data.get(i)); bw.write(line+addedColumn+lineSep);    }    }catch(Exception e){        System.out.println(e);    }finally  {        if(br!=null) br.close();        if(bw!=null) bw.close();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5490165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存