读取csv文件

<!--csv-->
        <dependency>
            <groupId>net.sourceforge.javacsv</groupId>
            <artifactId>javacsv</artifactId>
            <version>2.0</version>
        </dependency>
Map<String, CoordinateDto> map = new HashMap<>();

    @PostConstruct
    private Map <String, CoordinateDto> generatorCoordinate() throws Exception {
        log.info("读取结算单填充坐标点 xy.csv");
        Resource resource = new ClassPathResource("xy.csv");
        InputStream inputStream = resource.getInputStream();
        CsvReader csvReader = new CsvReader(inputStream, Charset.forName("utf-8"));
        csvReader.readHeaders(); //跳过第一行标题行
        while (csvReader.readRecord()) {
            String row = csvReader.getRawRecord(); //读取一整行数据
            String[] arr = row.split(",");
            map.put(arr[0], new CoordinateDto(Integer.parseInt(arr[1]), Integer.parseInt(arr[2])));
        }
        return map;
    }

 

posted @ 2020-04-15 23:30  Peter.Jones  阅读(291)  评论(0编辑  收藏  举报