首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义CSVFormat.RFC4180

自定义CSVFormat.RFC4180
EN

Stack Overflow用户
提问于 2018-05-07 14:59:38
回答 1查看 2K关注 0票数 0

我得读个csv文件。该文件可以包含任何分隔符,可以用“\”括起来。该文件也应该对RFC4180进行解析。(我知道在RFC4180中,分隔符是",",但用户也应该能够读取以“分部”分隔的文件。)

代码语言:javascript
运行
复制
public List<List<String>> readFileAsListOfList(File file, String delimiter, String lineEnding, String enclosure) throws Exception {
        if (!file.exists()) {
            throw new Exception("File doesn't exist.");
        }
        if (!file.isFile()) {
            throw new Exception("File must be a file.");
        }

        List<List<String>> fileContent = new ArrayList<>();
        CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(delimiter.charAt(0)).withEscape(lineEnding.charAt(0));
        if (StringUtils.isNotEmpty(enclosure)) {
            csvFormat.withQuote(enclosure.charAt(0));
        } else {
            csvFormat.withQuote(null);
        }
        System.out.println(csvFormat);
        List<String> lineContent = new ArrayList<>();
        for (CSVRecord rec : csvFormat.parse(new FileReader(file))) {
            for (String field : rec) {
                lineContent.add(field);
            }
            fileContent.add(lineContent);
        }
        return fileContent;
    }

如果我现在有这样的情况,即文件不包含,而且我有一行如下

aaa-bbb-“-ccc”

我得到以下错误:

线程"main“中的异常:读取下一条记录的IOException : org.apache.commons.csv.CSVParser$1.hasNext(CSVParser.java:540) at com.ids.dam.pim.validation.CSVFileReaderApache.readFileAsListOfList(CSVFileReaderApache.java:61) at com.ids.dam.pim.validation.CSVFileReaderApache.main(CSVFileReaderApache在封装令牌完成之前到达java.io.IOException:(startline 120707)(由: org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:288):(起始行120707) EOF在org.apache.commons.csv.Lexer.nextToken(Lexer.java:158) at org.apache.commons.csv.Lexer.nextToken(Lexer.java:158) at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:586) at org.apache.commons.csv.CSVParser$1.getNextRecord(CSVParser.java:527) .

我认为这是因为我的CSVFormat仍然包含一个双引号作为附件,因为这是默认的RFC4180。

打印出的格式如下:

Delimiter=<|> Escape= QuoteChar=<"> RecordSeparator=< > SkipHeaderRecord:false

对我来说,这意味着我可以用CSVFormat.RFC4180.withDelimiter(delimiter.charAt(0)...覆盖默认的分隔符,但不能将附件设置为null。

是否有一种方法可以在仍然使用RFC4180时将外壳设置为null?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-07 15:53:46

引用在CSV中始终是可选的,引用字符可以选择为分隔符。如果您知道您的文件使用|分隔符而不使用引号,则应该以这种方式构建CSVFormat。请注意,withOption(...)并不将该选项应用于当前的csv格式,而是返回与原始格式相同但具有选项集的现在格式。来自Apache CSVFormat javadoc

公众谘询委员会( public CSVFormat withQuoteMode,QuoteMode quoteModePolicy) 返回一个新的CSVFormat,其格式的输出引号策略设置为指定的值。 ..。 返回:一个新的CSVFormat ,与此相等,但具有指定的引号策略

你应该使用:

代码语言:javascript
运行
复制
    CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(delimiter.charAt(0))
            .withEscape(lineEnding.charAt(0));
    if (StringUtils.isNotEmpty(enclosure)) {
        csvFormat = csvFormat.withQuote(enclosure.charAt(0));
    } else {
        csvFormat = csvFormat.withQuoteMode(NONE);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50217170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档