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 ?
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.
You need to specify the encoding of the file you want to read.
try new InputStreamReader(new FileInputStream(...), <encoding>)
, or something like this instead of new FileReader(filename)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.