如何忽略超级csv中的未知列

时间:2016-08-08 02:13:20

标签: java csv supercsv

我使用超级csv的bean读取器来读取我的csv文件。如果POJO和列匹配,它可以正常工作。

例如,我的pojo有这些值。

//They have getters and setters.
//just to make it simple I have used public.
public myPojo
{
 public columnA;
 public columnB;
}

我的超级csv代码看起来像这样。

        final String[] header = beanReader.getHeader(true);
        int amountOfColumns=beanReader.length();
        CellProcessor[] processor = new CellProcessor[amountOfColumns];

所以,如果我的csv文件看起来像这样,它可以正常工作。

columnA,columnB

值1,值

但是如果我的csv文件有一个额外的列,它会因此异常而失败 检查相应的nameMapping元素是否与bean中的字段名称匹配。我可以避免此异常,只是忽略此列。基本上它说它找不到相应的setter。

unknownColumn,columnA,columnB

someValue中,值1,值

1 个答案:

答案 0 :(得分:1)

我使用listReader而不是bean reader。得到列值到地图,我能够做这样的事情。

08-07 21:34:43.264 26425-26775/ca.ubc.econ.montoya3.myapplication E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                                                                    Process: ca.ubc.econ.montoya3.myapplication, PID: 26425
                                                                                    java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                        at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                                        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                        at java.lang.Thread.run(Thread.java:818)
                                                                                     Caused by: java.lang.AssertionError: Exception should not have been thrown
                                                                                        at org.junit.Assert.fail(Assert.java:88)
                                                                                        at ca.ubc.econ.montoya3.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:236)
                                                                                        at ca.ubc.econ.montoya3.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:208)
                                                                                        at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                        at java.lang.Thread.run(Thread.java:818)