1

次のようなエントリを持つ非常に単純な tsv ファイルがあります。

614 2006-07-13 15:30:05 2009-11-20 23:56:21 510 350 3265    10  34
1038    2006-07-15 16:12:15 2009-11-16 05:12:11 304 443 4405    7   156
1437    2006-07-16 12:29:24 2009-11-16 16:25:12 45  73  725 6   37
2615    2006-07-19 23:23:55 2009-11-27 18:34:36 211 230 211 7   0
3148    2006-07-26 14:17:22 2009-11-20 17:35:18 7346    7244    11438   8   97
5593    2006-09-08 10:58:49 2009-11-24 06:08:27 898 1024    2897    8   56

ヘッダーがなく、別のソースから取得しているため、記述方法を制御できません。最初の列を読んで何かを行い、残りは無視したいと思います。

私のコードは次のとおりです。

    List<Long> userIds = new ArrayList<Long>();

    ICsvMapReader mapReader =  null;
    try {
        mapReader = new CsvMapReader(new FileReader(inFile), CsvPreference.TAB_PREFERENCE);  

        // only map the first column - setting header elements to null means those columns are ignored
        final String[] header = new String[] { "userid", null, null, null, null, null, null };

        final CellProcessor[] processors = new CellProcessor[] {null,
                null,
                null,
                null,
                null,
                null,
                null };

        Map<String, Object> userMap;
        while( (userMap = mapReader.read(header, processors)) != null ) {
            Long userId = Long.parseLong(userMap.get("userid").toString());
            userIds.add(userId);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        IOUtils.closeQuietly(mapReader);
    }

例外は発生しませんが、mapReader.read() 行は常に null を返します。プロセッサの最初の位置で null の代わりに new ParseLong() を使用しようとしましたが、効果はありませんでした。本当に基本的なものが欠けているように感じます。

4

2 に答える 2

0

nullデータには実際には8つの列があるため、ヘッダーとプロセッサに追加の要素を追加する必要がありましたが、あなたのコードは私にとってはうまく機能します。それ以外の場合、Super CSV は例外をスローします。

org.supercsv.exception.SuperCsvException: The number of columns 
to be processed (8) must match the number of CellProcessors (7): 
check that the number of CellProcessors you have defined matches 
the expected number of columns being read/written
context={lineNo=1, rowNo=1, columnNo=1, rowSource=
[614, 2006-07-13 15:30:05, 2009-11-20 23:56:21, 510, 350, 3265, 10, 34]}

正しいファイルを読み取っていることを確認します-空のファイルを読み取っているようです...

于 2014-05-22T13:01:30.767 に答える