All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fartherp.framework.poi.read.CSVRead Maven / Gradle / Ivy

There is a newer version: 3.0.6
Show newest version
/*
 * Copyright (c) 2017. CK. All rights reserved.
 */

package com.github.fartherp.framework.poi.read;

import au.com.bytecode.opencsv.CSVReader;
import com.github.fartherp.framework.poi.ReadDeal;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Author: CK
 * Date: 2016/1/17
 */
public class CSVRead {
    public void read(InputStream inputStream, CSVReaderDeal deal) {
        CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)));
        try {
            int tmp = deal.getBatchCount();
            List l = new ArrayList(tmp);
            int i = 0;
            String [] arr;
            while ((arr = reader.readNext()) != null) {
                ++i;
                if (i <= deal.skipLine()) {
                    continue;
                }
                E o = deal.dealBean(arr);
                if (o != null) {
                    l.add(o);
                    if (i % tmp == 0) {
                        deal.dealBatchBean(l);
                        l = new ArrayList(tmp);
                    }
                }
            }
            if (!l.isEmpty()) {
                deal.dealBatchBean(l);
            }
        } catch (IOException e) {
            // ignore
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // ignore
                reader = null;
            }
        }
    }

    public static interface CSVReaderDeal extends ReadDeal {
        /**
         * 一行CSV数据返回业务BEAN
         * @param arr 一行CSV数据
         * @return 业务BEAN
         */
        E dealBean(String[] arr);
    }

    public static abstract class DefaultCSVReaderDeal extends ReadDeal.DefaultReadDeal implements CSVReaderDeal {

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy