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

org.dromara.hutool.poi.csv.CsvReader Maven / Gradle / Ivy

There is a newer version: 6.0.0.M3
Show newest version
/*
 * Copyright (c) 2013-2024 Hutool Team and hutool.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hutool.poi.csv;

import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.func.SerConsumer;

import java.io.Closeable;
import java.io.File;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * CSV文件读取器,参考:FastCSV
 *
 * @author Looly
 * @since 4.0.1
 */
public class CsvReader extends CsvBaseReader implements Iterable, Closeable {
	private static final long serialVersionUID = 1L;

	private final Reader reader;

	//--------------------------------------------------------------------------------------------- Constructor start

	/**
	 * 构造,使用默认配置项
	 */
	public CsvReader() {
		this(null);
	}

	/**
	 * 构造
	 *
	 * @param config 配置项
	 */
	public CsvReader(final CsvReadConfig config) {
		this((Reader) null, config);
	}

	/**
	 * 构造,默认{@link #DEFAULT_CHARSET}编码
	 *
	 * @param file   CSV文件路径,null表示不设置路径
	 * @param config 配置项,null表示默认配置
	 * @since 5.0.4
	 */
	public CsvReader(final File file, final CsvReadConfig config) {
		this(file, DEFAULT_CHARSET, config);
	}

	/**
	 * 构造,默认{@link #DEFAULT_CHARSET}编码
	 *
	 * @param path   CSV文件路径,null表示不设置路径
	 * @param config 配置项,null表示默认配置
	 * @since 5.0.4
	 */
	public CsvReader(final Path path, final CsvReadConfig config) {
		this(path, DEFAULT_CHARSET, config);
	}

	/**
	 * 构造
	 *
	 * @param file    CSV文件路径,null表示不设置路径
	 * @param charset 编码
	 * @param config  配置项,null表示默认配置
	 * @since 5.0.4
	 */
	public CsvReader(final File file, final Charset charset, final CsvReadConfig config) {
		this(FileUtil.getReader(file, charset), config);
	}

	/**
	 * 构造
	 *
	 * @param path    CSV文件路径,null表示不设置路径
	 * @param charset 编码
	 * @param config  配置项,null表示默认配置
	 * @since 5.0.4
	 */
	public CsvReader(final Path path, final Charset charset, final CsvReadConfig config) {
		this(FileUtil.getReader(path, charset), config);
	}

	/**
	 * 构造
	 *
	 * @param reader {@link Reader},null表示不设置默认reader
	 * @param config 配置项,null表示默认配置
	 * @since 5.0.4
	 */
	public CsvReader(final Reader reader, final CsvReadConfig config) {
		super(config);
		this.reader = reader;
	}
	//--------------------------------------------------------------------------------------------- Constructor end
	/**
	 * 读取CSV文件,此方法只能调用一次
* 调用此方法的前提是构造中传入文件路径或Reader * * @return {@link CsvData},包含数据列表和行信息 * @throws IORuntimeException IO异常 */ public CsvData read() throws IORuntimeException { return read(this.reader, false); } /** * 读取CSV数据,此方法只能调用一次
* 调用此方法的前提是构造中传入文件路径或Reader * * @param rowHandler 行处理器,用于一行一行的处理数据 * @throws IORuntimeException IO异常 * @since 5.0.4 */ public void read(final SerConsumer rowHandler) throws IORuntimeException { read(this.reader, false, rowHandler); } /** * 根据Reader创建{@link Stream},以便使用stream方式读取csv行 * * @return {@link Stream} * @since 5.7.14 */ public Stream stream() { return StreamSupport .stream(spliterator(), false) .onClose(this::close); } @Override public Iterator iterator() { return parse(this.reader); } @Override public void close() { IoUtil.closeQuietly(this.reader); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy