如何在supercsv getHeader中使用编码

ale*_*x.g 5 java supercsv

我正在使用supercsv 2.1.0来解析其中包含德语单词的CSV文件.

给定的CSV文件在第一行有一个标题.在这个标题中有一些变异的元音,如:Ä,ä,Ü,ö等.例如:Betrag;Währung;信息

在我的编码中,我试图像这样得到csv的标题:

ICsvBeanReader inFile = new CsvBeanReader(new InputStreamReader(new FileInputStream(file), "UTF8"), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);

final String[] header = inFile.getHeader(true);
Run Code Online (Sandbox Code Playgroud)

这是我的标题数组的问题.使用utf8字符集不能正确编码带有变异元音的所有标题.

有没有办法正确读取标题?

这是一个伪单元测试:

public class TestSuperCSV {


@Test
public void test() {
    String path = "C:\\Umsatz.csv";
    File file = new File(path);

    try {
        ICsvBeanReader inFile = new CsvBeanReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"),
                CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
        final String[] header = inFile.getHeader(true);
        System.out.println(header[9]); //getting "W?hrung" but needed "Währung" here


    } catch (UnsupportedEncodingException | FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

亲切的问候,Alex

Jam*_*ett 5

听起来您的文件实际上并未使用 UTF-8 编码。

我可以通过使用 ISO-8859-1 编码创建 CSV 文件并运行您的代码来复制您的场景,它显示为W?hrung.

如果我随后将 更新InputStreamReader"ISO-8859-1"用作编码,则它正确显示为Währung.