1

I have a csv file in my assets file and would like to read it using opencsv
I successfully to get and read the file but fail in printing it,
and the string are displayed as the screen capture below.
My csv is save as Unicode from excel

My code in reading and logging csv:

AssetManager assetManager = context.getAssets();
    try {
        InputStream csvStream = assetManager.open("data.csv");
        InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
        CSVReader csvReader = new CSVReader(csvStreamReader);
        String [] nextLine;
        Log.d("test","reading csv");

        while ((nextLine = csvReader.readNext()) != null) {
            // nextLine[] is an array of values from the line
            Log.d("test",nextLine.toString() + " etc...");
            String[] temp= nextLine.toString().split(",");
            for(int i=0; i<nextLine.length;i++){
                Log.d("test", nextLine[i]);
            }
            break;
        }


    } catch (FileNotFoundException e) {
        Log.d("test","fail read csv");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("test","io exception csv");
        e.printStackTrace();
    }

the csv file's text
enter image description here

and the screen capture in the logcat:
enter image description here

brian661
  • 538
  • 3
  • 11
  • 31

1 Answers1

0

This depends on the encoding of the file.

With any luck, it's in UTF-8. You need to tell your InputStreamReader to use UTF-8 else it'll probably fall back to ISO 8859_1 (ISO-Latin-1).

You can do this like so:

InputStreamReader csvStreamReader = new InputStreamReader(csvStream, "UTF-8");

If your file is encoded in another format, you'll need to specify that.

Edit: After a re-read, I see you exported from Excel. You'll need to tell Excel to export in UTF-8 instead of whatever it defaults to (likely Windows-1252).

Community
  • 1
  • 1
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • Thanks for your quick reply and I successfully to read and print the text by using UTF-16, for the csv exporting from excel, may be because of version, I cannot tell what coding in exporting, and I export it by using the answer from http://superuser.com/questions/871189/excel-export-csv-file-with-japanese-character – brian661 Feb 03 '15 at 16:19