Trying to read Japanese CSV file in Java

I am trying to read a Japanese CSV file that is downloaded and extracted pragmatically.

Code for reading CSV

  String splitBy = ",";
            BufferedReader br;// = new BufferedReader(new FileReader(pathOfExcel + "\\KEN_ALL.CSV   "));
            br = new BufferedReader(new InputStreamReader(new FileInputStream(pathOfExcel + "\\KEN_ALL1.CSV"),"SHIFT-JIS")); 
              String line = "";
              int cnt = 0;
              while((line = br.readLine()) != null){
                  //System.out.println("Count :: " + cnt++);
                  List<Object> excelList = new ArrayList<Object>();
                  if(line != null){
                   String[] splitCells = line.split(splitBy);
                   excelList.add(splitCells[0].replace("\"", ""));
                   excelList.add(splitCells[1].replace("\"", ""));
                   excelList.add(splitCells[2].replace("\"", ""));
                   excelList.add(splitCells[3].replace("\"", ""));
                   excelList.add(splitCells[4].replace("\"", ""));
                   excelList.add(splitCells[5].replace("\"", ""));
                   excelList.add(splitCells[6].replace("\"", ""));
                   excelList.add(splitCells[7].replace("\"", ""));
                   excelList.add(splitCells[8].replace("\"", ""));
                   returnList.add(excelList);
                 }
              }
              br.close(); 

I tried both UTF-8 and SHIFT-JIS, as shown in the following code.

br = new BufferedReader(new InputStreamReader(new FileInputStream(pathOfExcel + "\\KEN_ALL1.CSV"),"UTF-8"));

When I tried to encode UTF-8 and SHIFT-JIS, it " excelList.add(splitCells[3].replace("\"", ""));"would return the following outputs. But where should the original result be ホ ッ カ イ ド ウ

UTF-8 - ί¶²ÄÞ³

Shift-jis - テ 篠 ッ ツ カ ツ イ テ 楪 ウ

+4
source share

Source: https://habr.com/ru/post/1618725/


All Articles