版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/79870560
该方法是读取csv文件的流操作
public static void main(String[] args) {
//获得文件路径
File csv = new File("E://export");
//获得文件下所有文件的集合
File[] files = csv.listFiles();
List<Map<String,String>> list = new ArrayList<>();
BufferedReader br = null;
//遍历集合
for(int i=0;i<files.length;i++){
if(files[i].isFile()){//如果是文件就进行读操作
String line = "";
//设置行标记,如果存在表头就从第二行进行读操作
int num = 0;
try {
//获得文件读流
br = new BufferedReader(new FileReader(files[i]));
while((line=br.readLine()) != null){
if(num > 0){
Map<String,String> map = new HashMap<>();
String[] str = line.split(",");
map.put("mpsId", str[6]);
map.put("mpsSubId", str[7]);
map.put("code", str[8]);
map.put("qty", str[13]);
map.put("batch", str[14]);
System.out.println(map);
list.add(map);
}
num++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(list.toString());
}