package org.springblade.dataextraction.kit;

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

import org.springframework.stereotype.Service;

/**
 * @Desc txt文件读取器
 * @Author William
 * @DateTime 2022 下午7:33:58
 */
@Service
public class TextReader {
	public List<String> read(InputStream is) {
		List<String> list = new ArrayList<>();
		String line = null;
		try {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			while ((line = br.readLine()) != null) {
				list.add(line);
			}

			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
}
