依赖
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
读取该文件夹下所有的csv文件
public ResponseState insertCSV() {
CsvReader reader = null;
int i = 0;
try {
File root = new File("D://csv");
File[] files = root.listFiles();
for (File file : files) {
reader = new CsvReader(file.toString(), ',', Charset.forName("GBK"));
while (reader.readRecord()) {
String[] strings = reader.getValues();
Orders orders = new Orders();
orders.setOrderid(UUID.randomUUID().toString());
orders.setOfficialAmount(strings[0]);
orders.setConsigneeName(strings[1]);
orders.setDeliveryAddress(strings[2]);
i = orderMapper.createOrder(orders);
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (i == 1) {
responseState.setState("201");
} else {
responseState.setState("401");
}
return responseState;
}