是否可以在Super CSV中一次读取两个文件?

时间:2014-04-13 13:21:03

标签: java csv supercsv

正如标题中的Super CSV可能会同时读取两个文件。下面的代码显示了一个file.csv的示例:

public class Reading {  
    private static final String CSV_FILENAME1 = "src/test/resources/test1/customers.csv";
    public static void partialReadWithCsvMapReader() throws Exception {     
        ICsvMapReader mapReader1 = null;
        try {
            mapReader1 = new CsvMapReader(new FileReader(CSV_FILENAME1), CsvPreference.STANDARD_PREFERENCE);            
            mapReader1.getHeader(true);
            final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
                null, null, "TOMEK" };
            final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                new NotNull(), new NotNull(), new NotNull(), new Optional(), new Optional(), new NotNull(),
                new NotNull(), new LMinMax(0L, LMinMax.MAX_LONG), new NotNull()};           
            Map<String, Object> customerMap;
            while( (customerMap = mapReader1.read(header, processors)) != null ) {
                System.out.println(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader1.getLineNumber(),
                    mapReader1.getRowNumber(), customerMap));
            }
        }
        finally {
            if( mapReader1 != null ) {
                mapReader1.close();
            }
        }
    }

}

一种解决方案是声明第二个变量CSV_FILENAME2:

private static final String CSV_FILENAME1 = "src/test/resources/test1/customers.csv";

等等,但还有其他可能性吗?感谢。

1 个答案:

答案 0 :(得分:0)

每个CsvReader只能读取一个Reader(作为构造函数参数传递) - 换句话说,您只能按CsvReader读取一个CSV文件。

正如JB Nizet所评论的那样 - 你真的需要立即定义你的意思&#39;。但是,无论是同时,顺序还是其他什么,您仍然需要两个CsvMapReader个实例。

是否要求一起处理文件(例如将2个CSV文件合并为1个),或者您只是在寻找处理多个文件的方法?