How read Japanese fields from CSV file into java beans?

▼魔方 西西 提交于 2019-12-12 03:18:31

问题


I've tried several popular CSV to java deserializers - OpenCSV, JSefa, and Smooks - none correctly read the file:

First Name,Last Name
エリック,山中
花子,鈴木
一郎,鈴木
裕子,田中
政治,山村

into my java object collection.

OpenCsv code:

    HeaderColumnNameTranslateMappingStrategy<Contact> strat = new HeaderColumnNameTranslateMappingStrategy<Contact>();
    strat.setType(Contact.class);
    strat.setColumnMapping(colNameTranslateMap);
    InputStreamReader fileReader=null;
    CsvToBean<Contact> csv = new CsvToBean<Contact>();
    fileReader = new InputStreamReader(new FileInputStream(file), "UTF-8");

    contacts = csv.parse(strat, new CSVReader(fileReader));

I've tried setting the Charset to UTF-8, UTF-16 and ISO-8859-1 when I create the FileInputStream, but the collection is never populated properly. As seen in the debugger and System.out the fields contain garbage and often the number of records is wrong.


回答1:


  1. FileInputStream is for reading streams of binary data, like an mp3 or PNG. Instead of a FIS, use a FileReader for reading streams of characters.
  2. To be blunt: who cares what charsets you tried using if they didn't work? You need to figure out what encoding the CSV file is actually using, and set that encoding when reading the file. To specify the encoding when using a FileReader:

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.



来源:https://stackoverflow.com/questions/8003333/how-read-japanese-fields-from-csv-file-into-java-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!