写/更改csv文件java [英] Writing/Altering csv file java

查看:229
本文介绍了写/更改csv文件java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载CSV文件,并保持活动而不关闭BufferReader。现在,我想在我的CSV中添加/删除更多行,并获取更新的CSV。这意味着我需要在更改CSV文件时动态通知。

I am loading a CSV file and kept alive without closing BufferReader. Now I want to add/delete few more rows to my CSV and get updated CSV. Which means I want dynamically notified when CSV file is altered.

以下是我的示例代码:

public class Check {


    public static void main(String args[]) throws IOException, InterruptedException{
        Check ch = new Check();
        //args[0] = "C:\\Users\\raponnam\\Desktop\\GxDefault.csv";
        String csvFile="C:\\Users\\raponnam\\Desktop\\GxDefault.csv";

        int lineCounter=0;
        if (csvFile!=null)
        {
            BufferedReader csvToRead = null;
            try{
                csvToRead = new BufferedReader(new FileReader(csvFile));
                String line;
                while ((line=csvToRead.readLine())!=null)
                {
                    lineCounter++;
                    String[] splitLine = line.split(",");
                    System.out.println("no of lines-->> "+lineCounter);
                }
                while(true)
                {

                    System.out.println("wait 6 seconds");
                    Thread.sleep(6000);
                }
            }finally{
                //csvToRead.close();
            }
        }
    }
}


推荐答案

CSV处理比你想象的要复杂得多。

CSV handling is far more complicated than you would first think.

导致噩梦的问题是不同的列分隔符, ,如果对列分隔符使用,它可能会在一些区域设置上使用双精度值,因为它用于表示小数),列有时用特殊字符'),转义进入图片等。

The issues causing the nightmares are the different column separators, locales (e.g., if you use , for column separators it might fire back with doubles on some locales, since it is used to denote the decimals), columns are sometimes wrapped with special characters (like " or '), escaping comes into the picture, etc.

如果您允许我提供建议,请使用成熟的CSV库来执行 OpenCSV (但是有很多的替代品)。使用它是非常微不足道,看到链接网站的例子。

If you allow me to have an advice, use a mature CSV lib for the task like OpenCSV (but there are tons of alternatives there). It's quite trivial to use, see the linked site for examples.

这篇关于写/更改csv文件java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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