简单读取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;

/**
 * 
 * 

Title: CsvUtils.java

* *

Description: *用于读取分隔符分隔文件的简单工具。默认分隔符为逗号。

* *

Company: 北京九恒星科技股份有限公司

* * @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> 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> 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> 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> 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> readCsv(BufferedReader input, final boolean mapTitles) throws IOException { if (input != null && input.ready()) { Map> entries = new TreeMap>(); List titles = new ArrayList(); 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 entryLine = new TreeMap(); 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 modifiedLine = new ArrayList(); 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 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 * @author luhao * @since:2019年6月13日 下午2:48:01 */ public static List getRecordMapFromCsv(String fileInput, int sheetId, int beginRow, int beginCol) { List result = new ArrayList(); try { Map> elementsFromCsv = getElementsFromCsv(fileInput,false); Map 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> csvElements = CsvUtils
                    .getElementsFromCsv("C:/Users/Administrator/Desktop/_aaa.csv", false);
            System.out.println(csvElements);
            
            
        } catch (FileNotFoundException e) {
            System.exit(-1);
        }
        System.exit(1);
    }

 

你可能感兴趣的:(简单读取CSV文件的工具)