This is my java code
(and the whole project has UTF-8 encoding)
public static ArrayList<ArrayList<String>> getCSVContent(String filepath) throws Exception {
CSVReader reader = new CSVReader(new FileReader(filepath));
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < nextLine.length; i++) {
list.add(nextLine[i]);
}
array.add(list);
}
reader.close();
return array;
}
This is my CSV File:
Place1 ул. "Цанко Церковски" No37 Category1 bar Bulgaria Sofia 310-808-5243
Place 2 ул."Ген. Гурко" No 6 Category2 bar Bulgaria Sofia 415-846-1688
Place 3 ул. "Гео Милев" No 18 Category3 bar Bulgaria Sofia 720-318-9049
And this is the output
instead of ул. "Цанко Церковски" No37, for example
I get: . , .
It probably has something to do with the encoding of the CSV file, But Im not sure how can I view/change that and whether I should use Word or Open Office?
Also, may I change the way Java reads such files so even if they are in the wrong encoding, Java fixes it?
This is quite possibly the problem:
CSVReader reader = new CSVReader(new FileReader(filepath));
FileReader
always uses the platform-default encoding. I prefer to use InputStreamReader
wrapped around a FileInputStream
, as then you can specify the encoding:
try (InputStream stream = new FileInputStream(filepath)) {
CSVReader reader = new CSVReader(new InputStreamReader(stream, "UTF-8"));
...
}
您可以像这里一样显式设置编码
new CSVReader(new InputStreamReader(new FileInputStream(filePath), encoding));
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.