Home Tags Posts tagged with "文件"

文件

0 55
/**
 * 获取CVS文件
 * @return 待刷入的数据
 */
private List<DistrictInfo> readByCSV(){
    log.info("获取CVS文件内容");
    ArrayList<DistrictInfo> list = new ArrayList<>();
    try (Reader reader = Files.newBufferedReader(Paths.get(""), Charset.forName("GBK"))) {
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader);
        for (CSVRecord record : records) {
            DistrictInfo districtInfo = new DistrictInfo();
            districtInfo.setRegionId(Integer.valueOf(record.get(0)));
            districtInfo.setRegionName(record.get(1));
            districtInfo.setpRegionId(Integer.valueOf(record.get(2)));
            districtInfo.setClasses(Integer.valueOf(record.get(3)));
            districtInfo.setStatus(Integer.valueOf(record.get(4)));
            districtInfo.setOperator(record.get(5));
            districtInfo.setMemo(record.get(6));
            districtInfo.setVersion(Integer.valueOf(record.get(7)));
            list.add(districtInfo);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return list;
}

/** 获取CVS文件 
 *  
 */
public void writeToCSV(List <QueryBusinessFieldListResp> list){
    final String FILE_NAME = "businessIDPointFromZT.csv";
    final String[] FILE_HEADER = {"BusinessID"};
    // 这里显式地配置一下CSV文件的Header,然后设置跳过Header(要不然读的时候会把头也当成一条记录)
    CSVFormat format = CSVFormat.DEFAULT.withHeader(FILE_HEADER).withSkipHeaderRecord();
    // 这是写入CSV的代码
    try(Writer out = new FileWriter(FILE_NAME);
        CSVPrinter printer = new CSVPrinter(out, format)) {
        for (QueryBusinessFieldListResp resp : list) {
            if (resp != null) {
                for (QueryBusinessFieldResp queryBusinessFieldResp : resp.getBusinessFieldRespList()) {
                    List<String> records = new ArrayList<>();
                    records.add(queryBusinessFieldResp.getBusinessId());
                    records.add(queryBusinessFieldResp.getValue().toString());
                    printer.printRecord(records);
                }
            }
        }
        } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}