1
gxm44 2021-10-28 14:14:14 +08:00
你这里的“读取 CSV 遇到乱码”,是程序里读出来的 csv 是乱码?
|
3
e583409 OP BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
CSVParser parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); List<String> headers = parser.getHeaderNames(); List<CSVRecord> dataList = parser.getRecords(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "GBK"));//"UTF-8" parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); headers = parser.getHeaderNames(); dataList = parser.getRecords(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));//"UTF-8" parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); headers = parser.getHeaderNames(); dataList = parser.getRecords(); |
4
SoloCompany 2021-10-28 18:17:04 +08:00 via iPhone
bom detect 加 utf8 detect 加 gb18030 fallback
|
5
e583409 OP @SoloCompany 谢谢 目前我是这样做的 只是觉得不够优雅 好像也么有其他办法了
|
6
qiancheng 2023-09-27 17:21:27 +08:00
```python
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)))); CSVParser parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); List<String> headers = parser.getHeaderNames(); List<CSVRecord> dataList = parser.getRecords(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "GBK"));//"UTF-8" parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); headers = parser.getHeaderNames(); dataList = parser.getRecords(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));//"UTF-8" parser = CSVParser.parse(br, CSVFormat.EXCEL.builder().setHeader().setSkipHeaderRecord(true).build()); headers = parser.getHeaderNames(); dataList = parser.getRecords(); ``` |