代码如下:
@Test
public void TESTt() {
File csv = new File("D:\\2021-04.csv");
//File csv = new File("D:\\cs.csv");
csv.setReadable(true);
csv.setWritable(true);
InputStreamReader isr = null;
try {
try {
isr = new InputStreamReader(new FileInputStream(csv), "UTF-8");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
BufferedReader br = new BufferedReader(isr);
String line = "";
ArrayList<String> records = new ArrayList<>();
//csv表格读取行数
while (true) {
try {
if (!((line = br.readLine()) != null)) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
records.add(line);
}
System.out.println(line);
System.out.println("csv表格读取行数:" + records.size());
}
乱码如下:
换字符集试试
csv文件用poi或者easyexcel读,要都能用流读,还要这些api干嘛