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

org.beangle.commons.transfer.csv.CsvItemReader Maven / Gradle / Ivy

The newest version!
/*
 * Beangle, Agile Development Scaffold and Toolkits.
 *
 * Copyright © 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package org.beangle.commons.transfer.csv;

import java.io.IOException;
import java.io.LineNumberReader;

import org.beangle.commons.io.IOs;
import org.beangle.commons.lang.Strings;
import org.beangle.commons.transfer.io.ItemReader;
import org.beangle.commons.transfer.io.TransferFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 

* CsvItemReader class. *

* * @author chaostone * @version $Id: $ */ public class CsvItemReader implements ItemReader { /** * Commons Logging instance. */ protected static final Logger logger = LoggerFactory.getLogger(CsvItemReader.class); LineNumberReader reader; /** 标题所在行 */ private int headIndex = 0; /** 数据起始行 */ private int dataIndex = 1; private int indexInCsv = 1; /** *

* Constructor for CsvItemReader. *

* * @param reader a {@link java.io.LineNumberReader} object. */ public CsvItemReader(LineNumberReader reader) { this.reader = reader; } /** *

* readDescription. *

* * @return an array of {@link java.lang.String} objects. */ public String[] readDescription() { return null; } /** *

* readTitle. *

* * @return an array of {@link java.lang.String} objects. */ public String[] readTitle() { try { reader.readLine(); return Strings.split(reader.readLine(), ","); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } private void preRead() { while (indexInCsv < dataIndex) { try { reader.readLine(); } catch (IOException e1) { logger.error("read csv", e1); } indexInCsv++; } } /** *

* read. *

* * @return a {@link java.lang.Object} object. */ public Object read() { preRead(); String curData = null; try { curData = reader.readLine(); } catch (IOException e1) { logger.error(curData, e1); } indexInCsv++; if (null == curData) { return null; } else { return Strings.split(curData, ","); } } /** *

* Getter for the field headIndex. *

* * @return a int. */ public int getHeadIndex() { return headIndex; } /** {@inheritDoc} */ public void setHeadIndex(int headIndex) { if (this.dataIndex == this.headIndex + 1) { setDataIndex(headIndex + 1); } this.headIndex = headIndex; } /** *

* Getter for the field dataIndex. *

* * @return a int. */ public int getDataIndex() { return dataIndex; } /** {@inheritDoc} */ public void setDataIndex(int dataIndex) { if (this.dataIndex == this.indexInCsv) { this.dataIndex = dataIndex; this.indexInCsv = dataIndex; } } /** *

* getFormat. *

* * @return a {@link java.lang.String} object. */ public TransferFormat getFormat() { return TransferFormat.Csv; } @Override public void close() { IOs.close(reader); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy