java,通过common-csv解析网络下载url,将其解析为csv数据并转换成bean

183 阅读1分钟

maven引入


<groupId>org.apache.commons</groupId>

<artifactId>commons-csv</artifactId>

<version>1.9.0</version>

</dependency>

代码实现

           //测试使用的csv下载地址
            String csvUrl = " http://sdxkyy.topwellsoft.com:8080/test.csv";
            //创建CSVFormat对象
            CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',');
            //初始化CSVParser 对象
            CSVParser parse = CSVParser.parse(new URL(csvUrl), Charset.forName("GBK"), format);

            /*遍历解析的csv每一行数据,转换成实体*/
            for (CSVRecord record : parse) {
                log.info(record.toString());
            }
 finally {
            //关闭
            if (null != parse) {
                parse.close();
            }
        }