16

I'm trying to parse a .csv file with OpenCSV in NetBeans 6.0.1. My file contains some Unicode character. When I write it in output the character appears in other form, like (HJ1'-E/;). When when I open this file in Notepad, it looks ok.

The code that I used:

CSVReader reader=new CSVReader(new FileReader("d:\\a.csv"),',','\'',1);
    String[] line;
    while((line=reader.readNext())!=null){
        StringBuilder stb=new StringBuilder(400);
        for(int i=0;i<line.length;i++){
            stb.append(line[i]);
            stb.append(";");
        }
        System.out.println( stb);
    }
Stevoisiak
  • 16,510
  • 19
  • 94
  • 173
meysam_pro
  • 215
  • 1
  • 2
  • 13

1 Answers1

37

First you need to know what encoding your file is in, such as UTF-8 or UTF-16. What's generating this file to start with?

After that, it's relatively straightforward - you need to create a FileInputStream wrapped in an InputStreamReader instead of just a FileReader. (FileReader always uses the default encoding for the system.) Specify the encoding to use when you create the InputStreamReader, and if you've picked the right one, everything should start working.

Note that you don't need to use OpenCSV to check this - you could just read the text of the file yourself and print it all out. I'm not sure I'd trust System.out to be able to handle non-ASCII characters though - you may want to find a different way of examining strings, such as printing out the individual values of characters as integers (preferably in hex) and then comparing them with the charts at unicode.org. On the other hand, you could try the right encoding and see what happens to start with...

EDIT: Okay, so if you're using UTF-8:

CSVReader reader=new CSVReader(
    new InputStreamReader(new FileInputStream("d:\\a.csv"), "UTF-8"), 
    ',', '\'', 1);
String[] line;
while ((line = reader.readNext()) != null) {
    StringBuilder stb = new StringBuilder(400);
    for (int i = 0; i < line.length; i++) {
         stb.append(line[i]);
         stb.append(";");
    }
    System.out.println(stb);
}

(I hope you have a try/finally block to close the file in your real code.)

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Im getting `The constructor CSVReader(InputStreamReader, char) is undefined` with this solution – Code_Mode Jun 06 '18 at 07:38
  • @Code_Mode: My code doesn't try to call such a constructor - it tries to call `CSVReader(InputStreamReader, char, char, int)`. Does *that* work for you? – Jon Skeet Jun 06 '18 at 08:28
  • Even for this , getting same error. I am using OpenCSV4.1 and its constructor does not have this constructor – Code_Mode Jun 06 '18 at 08:50
  • @Code_Mode: Looking at the documentation and the [source](https://sourceforge.net/p/opencsv/source/ci/master/tree/src/main/java/com/opencsv/CSVReader.java#l145), it should be there, but deprecated. Consider using CSVReaderBuilder instead. – Jon Skeet Jun 06 '18 at 09:17