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

com.github.rrsunhome.excelsql.parser.TextFileParser Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package com.github.rrsunhome.excelsql.parser;

import com.github.rrsunhome.excelsql.config.BaseParserConfig;
import com.github.rrsunhome.excelsql.config.TextParserConfig;
import com.github.rrsunhome.excelsql.parser.support.ArrayRowResultSet;
import com.github.rrsunhome.excelsql.parser.support.BaseRowResultSet;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangqijia
 * create at:  2021/11/7  下午7:08
 */
public class TextFileParser extends AbstractFileParser {

    private static final String DEFAULT_CELL_DELIMITER = "\t";

    @Override
    public String[] getSupportedFileExtensions() {
        return new String[]{"text", "txt"};
    }


    @Override
    public BaseParserConfig getDefaultParserConfig() {
        return new TextParserConfig(DEFAULT_CELL_DELIMITER);
    }

    @Override
    protected List load(InputStream is, BaseParserConfig parserConfig) throws Exception {
        TextParserConfig textParserConfig = (TextParserConfig) parserConfig;
        List rowResultSets = new ArrayList<>(16);


        int rowIndex = 0;
        for (String line : readAllLines(is, StandardCharsets.UTF_8)) {
            // 跳过标题行
            if (rowIndex <= parserConfig.getTitleRowIndex()) {
                rowIndex++;
                continue;
            }

            String[] lines = line.split(textParserConfig.getDelimiter());
            rowResultSets.add(new ArrayRowResultSet(rowIndex, lines));
            rowIndex++;
        }
        return rowResultSets;
    }


    public static List readAllLines(InputStream is, Charset cs) throws IOException {
        try (BufferedReader reader = newBufferedReader(is, cs)) {
            List result = new ArrayList<>();
            for (; ; ) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                result.add(line);
            }
            return result;
        }
    }

    public static BufferedReader newBufferedReader(InputStream is, Charset cs)
            throws IOException {
        CharsetDecoder decoder = cs.newDecoder();
        Reader reader = new InputStreamReader(is, decoder);
        return new BufferedReader(reader);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy