简单读取CSV文件的工具

参考:https://github.com/vegaasen/knowit-julekalender

package com.nstc.aims.controller.business.recordmould;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.nstc.aims.model.RecordMap;

/**
 * 
 * <p>Title: CsvUtils.java</p>
 *
 * <p>Description: *用于读取分隔符分隔文件的简单工具。默认分隔符为逗号。</p>
 *
 * <p>Company: 北京九恒星科技股份有限公司</p>
 *
 * @author luhao copy from https://github.com/vegaasen/csv-importer-utils
 * 
 * @since:2019年6月13日 下午1:19:22
 *
 */
public final class CsvUtils {

    /** 默认分隔字符 */
    public static final char DEFAULT_DELIMITER = ',';
    
    /** 默认编码集 */
    public static final Charset DEFAULT_CHARSET = Charset.defaultCharset();

    /** 空字符串 */
    private static final String EMPTY = "";
    
    /** 单引号字符 */
    private static final char HYPHEN = '"';
    
    /** 占位字符  */
    private static final char PLACEHOLDER = '\u02B0';
    
    /** 占位字符串 */
    private static final String PLACEHOLDER_STR = String.valueOf(PLACEHOLDER);

    /** 分隔符 */
    private static String delimiter = String.valueOf(DEFAULT_DELIMITER);
    
    /** 分隔符字符 */
    private static char delimiter_char = DEFAULT_DELIMITER;
    
    /** 编码集 */
    private static Charset charset = DEFAULT_CHARSET;
    
    /** 是否忽略第一行 */
    private static boolean omitFirstLine = false;

    private CsvUtils() {
    }
    
    /** 获得表格元素 */
    public static Map<Integer, Map<String, String>> getElementsFromCsv(final String location) throws FileNotFoundException {
        return getElementsFromCsv(location, false);
    }

    /**
     * @param location  The location
     * @param mapTitles true == map attributes to the titles (if any) found on the top level of the csv.
     * @return a map containing the line, and its mapping wihting that line. Simple.
     * @throws FileNotFoundException _
     */
    public static Map<Integer, Map<String, String>> getElementsFromCsv(final String location, final boolean mapTitles) throws FileNotFoundException {
        if (!"".equals(location)) {
            try {
                return getElementsFromCsv(FileUtils.getFile(location), mapTitles);
            } catch (IOException e) {
                throw new FileNotFoundException("File not found.");
            }
        }
        throw new IllegalArgumentException("Missing argument.");
    }

    public static Map<Integer, Map<String, String>> getElementsFromCsv(final File file) {
        return getElementsFromCsv(file, false);
    }

    /**
     * @param file      The file
     * @param mapTitles true == map attributes to the titles (if any) found on the top level of the csv.
     * @return a map containing the line, and its mapping wihting that line. Simple.
     */
    public static Map<Integer, Map<String, String>> getElementsFromCsv(final File file, final boolean mapTitles) {
        if (file != null) {
            try {
                return readCsv(FileUtils.getBufferedReader(file), mapTitles);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        throw new IllegalArgumentException("Missing argument.");
    }

    private static Map<Integer, Map<String, String>> readCsv(BufferedReader input, final boolean mapTitles) throws IOException {
        if (input != null && input.ready()) {
            Map<Integer, Map<String, String>> entries = new TreeMap<Integer, Map<String, String>>();
            List<String> titles = new ArrayList<String>();
            String currentLine;
            boolean completedFirstLine = false/*, omitted = false*/;
            int counter = 0, position = 0;
            while ((currentLine = input.readLine()) != null) {
                if (!"".equals(currentLine)) {
                    currentLine = massageLine(currentLine);
                    if (mapTitles) {
                        if (!completedFirstLine) {
                            if (currentLine.contains(delimiter)) {
                                Collections.addAll(titles, currentLine.split(delimiter));
                            } else {
                                throw new RuntimeException(
                                        String.format("Unable to find {%s} as delimiter. Wrong delimiter?", delimiter)
                                );
                            }
                            completedFirstLine = true;
                        }
                    }
                    final Map<String, String> entryLine = new TreeMap<String, String>();
                    if (omitFirstLine) {
                        omitFirstLine = false;
                        continue;
                    }
                    for (String entry : currentLine.split(delimiter)) {
                        if (entry.contains(PLACEHOLDER_STR)) {
                            entry = entry.replaceAll(PLACEHOLDER_STR, delimiter);
                        }
                        entryLine.put(
                                ((mapTitles) ? titles.get(position) : String.valueOf(position)),
                                ((charset != DEFAULT_CHARSET) ? new String(entry.getBytes(), charset) : entry)
                        );
                        position++;
                    }
                    entries.put(counter, entryLine);
                    position = 0;
                    counter++;

                }
            }
            return entries;
        }
        return Collections.emptyMap();
    }

    private static String massageLine(final String line) {
        if (!"".equals(line)) {
            if (line.contains("\"")) {
                ArrayList<Character> modifiedLine = new ArrayList<Character>();
                boolean withinHyphenSequence = false;
                for (int i = 0; i < line.length(); i++) {
                    if (line.charAt(i) == HYPHEN) {
                        withinHyphenSequence = ((!withinHyphenSequence));
                    }
                    if (withinHyphenSequence) {
                        if (line.charAt(i) == delimiter_char) {
                            modifiedLine.add(PLACEHOLDER);
                        } else {
                            modifiedLine.add(line.charAt(i));
                        }
                    } else {
                        modifiedLine.add(line.charAt(i));
                    }
                }
                return getStringFromList(modifiedLine);
            }
            return line;
        }
        return EMPTY;
    }

    private static String getStringFromList(ArrayList<Character> list) {
        StringBuilder sb = new StringBuilder();
        for (char s : list) {
            sb.append(s);
        }
        return sb.toString();
    }

    public static void setDelimiter(final String del) {
        if (!"".equals(del)) {
            delimiter = del;
            delimiter_char = del.charAt(0);
        }
    }

    public static void setCharset(final Charset chSet) {
        charset = chSet;
    }

    public static void setOmitFirstLine(final boolean choice) {
        omitFirstLine = choice;
    }

    /**
     * 
     * @Description:组装Excel中的标题行
     * @param fileName 文件路径
     * @param sheetId sheet页,从0开始
     * @param beginRow 始读行,从0开始
     * @param beginCol 始读列,从0开始
     * @return List<RecordMap>
     * @author luhao
     * @since:2019年6月13日 下午2:48:01
     */
    public static List<RecordMap> getRecordMapFromCsv(String fileInput, int sheetId, int beginRow, int beginCol) {
        List<RecordMap> result = new ArrayList<RecordMap>();
        try {
            Map<Integer, Map<String, String>> elementsFromCsv = getElementsFromCsv(fileInput,false);
            Map<String, String> line = elementsFromCsv.get(beginRow);
            int cols = line.size();
            for (int i = beginCol; i < cols && beginCol < cols; i++) {
                RecordMap recordMap = new RecordMap();
                recordMap.setColName(line.get(String.valueOf(i)));
                recordMap.setColIndex(i);
                result.add(recordMap);
            }
            
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

}
final class FileUtils {

    private static final int DEFAULT_BUFFER_SIZE = 65536;
    private static int bufferSize = DEFAULT_BUFFER_SIZE;

    public static File getFileFromClassPath(final String fileName) {
        if (!"".equals(fileName)) {
            final URL url = FileUtils.class.getResource(fileName);
            if (url != null) {
                String resource = null;
                try {
                    if("file".equals(url.getProtocol())) {
                        resource = url.getFile();
                    }else if("jar".equals(url.getProtocol())) {
                        JarURLConnection connection = (JarURLConnection) url.openConnection();
                        resource = connection.getJarFile().getName();
                    }
                    return new File(resource);
                } catch (Exception e) {
                    //eating the error..
                }
            }
            return null;
        }
        throw new IllegalArgumentException("Missing argument fileName.");
    }

    public static File getFile(final String filename) throws FileNotFoundException {
        if (!"".equals(filename)) {
            final File file = new File(filename);
            if (file.exists()) {
                return file;
            }
            throw new FileNotFoundException(String.format("Unable to find file %s", filename));
        }
        throw new IllegalArgumentException("Missing argument fileName.");
    }

    public static BufferedReader getBufferedReader(final File file) throws FileNotFoundException {
        if (file != null) {
            return new BufferedReader(new FileReader(file));
        }
        throw new IllegalArgumentException("Missing argument");
    }

    public static BufferedInputStream readStreamFromFile(final File file) throws IOException {
        //final int size = (int) file.length();
        try {
            return new BufferedInputStream(new FileInputStream(file), bufferSize);
        } catch (final FileNotFoundException e) {
            throw new IOException("File not found");
        }
    }

    public static BufferedInputStream readStreamFromFile(final String filename) throws IOException {
        final File file = new File(filename);
        return readStreamFromFile(file);
    }

    public static byte[] readBytesFromFile(final String filename) throws IOException {
        final File file = new File(filename);
        final int size = (int) file.length();
        final byte[] buff = new byte[size];
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file), bufferSize);
            in.read(buff, 0, size);
            return buff;
        } catch (final FileNotFoundException e) {
            throw new IOException("File `" + filename + "' not found");
        } catch (final IOException e) {
            throw new IOException("Error writing file `" + filename + "'", e);
        } finally {
            closeSilently(in);
        }
    }

    public static void closeSilently(final Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (final IOException e) {
            //
        }
    }

}

测试类

    public static void main(String... args) {
        try {
            CsvUtils.setDelimiter(",");
            CsvUtils.setCharset(Charset.forName("GBK"));
            Map<Integer, Map<String, String>> csvElements = CsvUtils
                    .getElementsFromCsv("C:/Users/Administrator/Desktop/_aaa.csv", false);
            System.out.println(csvElements);
            
            
        } catch (FileNotFoundException e) {
            System.exit(-1);
        }
        System.exit(1);
    }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值