[英]OpenCSV parsing does not show special character
我正在嘗試在jTable中加載.csv文件。 在記事本中,文件顯示OK,但在jTable中,一些字符如“£”,“$”變成一個框。
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);
}
}
我怎么解決這個問題 ?
將文件的編碼與InputStreamReader
使用的編碼匹配,例如,如果您的文件是ISO-8859-1,則可以使用
CSVReader reader = new CSVReader(
new InputStreamReader(new FileInputStream(filename), "ISO-8859-1"));
UTF-8需要2個字節才能顯示字符,而ISO-8859-1只需要1.如果使用UTF-8讀取ISO-8859-1編碼文件,那么如果用后者顯示,則£
等字符將無法正確顯示。
閱讀: 字符編碼的粗略指南
您需要指定要讀取的文件的編碼。
嘗試new InputStreamReader(new FileInputStream(...), <encoding>)
,或類似的東西,而不是new FileReader(filename)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.