简体   繁体   English

OpenCSV CSVReader UTF-8编码

[英]OpenCSV CSVReader UTF-8 encoding

I'm trying to read a CSV file with the following characters: â/ô/etc. 我正在尝试读取包含以下字符的CSV文件:â/ô/ etc。 My code isn't parsing these characters well. 我的代码无法很好地解析这些字符。 I'm getting the character/symbol instead of the real character. 我得到的是-字符/符号而不是真实字符。

This is the code I'm using for reading the CSV file: 这是我用来读取CSV文件的代码:

 private List<String[]> getRows(File f) throws IOException {
    //FileReader fileReader = new FileReader(f);
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(f), "UTF-8");

    try {
        CSVReader reader = new CSVReader(inputStreamReader, ';');

        try {
            return reader.readAll();
        } finally {
            reader.close();
        }
    } finally {
        inputStreamReader.close();
    }
}

Who can help me? 谁能帮我? Thanks! 谢谢!

Try something like below. 尝试以下类似的方法。

  File file = new File("H:\\file name.csv");


  BufferedReader br = new BufferedReader(new FileReader(file));
  int lineNumber = 0; 

  while ((line = br.readLine()) != null) {

    lineNumber++;


    if ( line.trim().length() == 0 ) {  
        continue;  
      }

    arr=line.split(",");

   for (int j=0;j<arr.length;j++)
    {
     ft=arr[j];
     ft=ft.trim();

   System.out.print(ft);     

    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM