0

解析使用 UTF8 编码的文件时出现问题。

我有两个完全相同的文件,除了它们的编码。(我只是简单地复制了文件并用 UTF8 保存,所以内容是相同的)。一种使用 ANSI 编码,另一种使用 UTF8 编码。使用 ANSI 编码的文件被成功解析,而另一个文件导致 BeanIO 在调用 BeanReader.read() 方法时抛出 UnidentifiedRecordException:

org.beanio.UnidentifiedRecordException: Unidentified record at line 1

我试图通过使用以下代码将编码显式设置为 UTF8 来解决此问题:

public static BeanReader getBeanReader(File file, StreamBuilder builder) {
     StreamFactory factory = StreamFactory.newInstance();
     factory.define(builder);
     InputStream iStream;
     try {
         iStream = new FileInputStream(file);
     } catch (FileNotFoundException e) {
         throw new CustomException("Could not create BeanReader, file not found", e);
     }
     Reader reader = new InputStreamReader(iStream, StandardCharsets.UTF_8);
     return factory.createReader("reader", reader);
}

这并不能解决问题。

此错误的原因可能是什么?

4

1 回答 1

0

由于声称第一行错误,您是否在没有 BOM的情况下保存了 UTF-8 (文件开始时臭名昭著的零宽度空间)?

于 2015-08-03T08:35:10.047 回答