Question

I am trying to load a .csv file in jTable. In notepad the file shows OK but in jTable some characters like "£", "$" becomes a box.

private void parseUsingOpenCSV(String filename){

DefaultTableModel model = (DefaultTableModel) empTbl.getModel();
int rows = model.getRowCount();
if (rows>0){
for(int i=0 ; i<rows; i++)
{model.removeRow(0);}
}   

    try {
        CSVReader reader = new CSVReader(new FileReader(filename));
        String [] nextLine;
        int rowNumber = 0;

        while ((nextLine = reader.readNext()) != null) {
            rowNumber++;
                model.addRow(new Object[]{nextLine[0], nextLine[1], nextLine[2], nextLine[3], nextLine[4], nextLine[5], nextLine[6], nextLine[7]});
        }
    } catch (IOException e) {
    System.out.println(e);
    }
}

How can I solve this problem ?

Was it helpful?

Solution

Match the encoding of your file to that used by the InputStreamReader, for example if your file is ISO-8859-1, you can use

CSVReader reader = new CSVReader(
        new InputStreamReader(new FileInputStream(filename), "ISO-8859-1")); 

UTF-8 requires 2 bytes to display a character whereas ISO-8859-1 only required 1. If an ISO-8859-1 encoded file is read using UTF-8, then characters such as £ will not display correctly if displayed with the latter.

Read: A Rough Guide to Character Encoding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top