java读取CSV文件,去除bom表头

    // 重要包出处
    import com.csvreader.CsvReader;
	import com.google.common.collect.Lists;
	import jodd.util.CsvUtil;
	import org.apache.commons.io.input.BOMInputStream;
    import org.apache.commons.beanutils.PropertyUtils;

    /**
     * 读取Csv文件
     *
     * @param im  输入流
     * @param clazz 映射的实体类
     * @param columnMap csv表头对应的字段名称
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> readCsvToList(InputStream im, Class<T> clazz, Map<String, String> columnMap)
        throws Exception {
        List<String[]> csvFileList = new ArrayList<>();
        CsvReader reader = new CsvReader(new InputStreamReader(new BOMInputStream(im)));
        // 读取表头,跳过表头 如果需要表头的话,这句可以忽略
        reader.readHeaders();
        String[] headArray = reader.getHeaders();
        // 逐行读入除表头的数据
        int i = 0;
        while (reader.readRecord()) {
            String rawRecord = reader.getRawRecord();
            String[] record = CsvUtil.toStringArray(rawRecord);

            csvFileList.add(record);
            log.info("读取" + i + "行");
            i++;
        }
        reader.close();
        if (ListUtils.listIsNull(csvFileList)) {
            log.info("csv文件为空");
            return null;
        }
        int size = csvFileList.size();
        List<T> resultList = Lists.newArrayList();
        for (int i1 = 0; i1 < size; i1++) {
            String[] datas = csvFileList.get(i1);

            T t = clazz.newInstance();
            Set<Map.Entry<String, String>> entries = columnMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String key = entry.getKey();
                int idx = ArrayUtils.findIndex(headArray, key);
                if (idx == -1) {
                    continue;
                }
                String data = datas[idx];
                String field = entry.getValue();
                PropertyUtils.setProperty(t, field, data);
            }
            resultList.add(t);
        }
        return resultList;
    }
    public static <T> int findIndex(T[] array, T t) throws UnsupportedEncodingException {
        int idx = -1;
        if (Objects.isNull(t)) {
            return idx;
        }
        for (int i = 0; i < array.length; i++) {
            if (Objects.nonNull(array[i]) && Objects.equals(t, array[i])) {
                idx = i;
                break;
            }
        }
        return idx;
    }

调用

    @Test
    public void test02() throws Exception {
        FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\Desktop\\locations.csv"));
        Map<String, String> excelMap = new HashMap<>();
        excelMap.put("经度", "lon");
        excelMap.put("纬度", "lat");
        excelMap.put("城市", "city");
        excelMap.put("地址", "address");
        List<ImportLocationsModel> importLocationsModels =
            readCsvToList(fileInputStream, ImportLocationsModel.class, excelMap);
        System.out.println(JSONObject.toJSONString(importLocationsModels));
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值