CSV 文件解析

介绍

    在很多时候,数据是以CSV文件格式存放的。在提取CSV数据时,我们借助javacsv这个开源工具来处理,还是比较方便。


javacsv in pom.xml of Maven

		<dependency>
			<groupId>net.sourceforge.javacsv</groupId>
			<artifactId>javacsv</artifactId>
			<version>2.0</version>
		</dependency>


import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import util.CommonTool;

import com.csvreader.CsvReader;

/**
 * @author shengshu
 * 
 */
public class CsvCmHandle {
	private static final Logger logger = LogManager.getLogger(CsvCmHandle.class);

	private static Map<String, Map<String, String>> cmMap = new ConcurrentHashMap<String, Map<String, String>>();

	/**
	 * *
	 * <p>
	 * Read CSV file and store records into Map.
	 * 
	 * @param csvFilePath
	 * @return
	 */
	public static Map<String, Map<String, String>> csvCmHandle(String csvFilePath) {
		logger.info("CSV File: " + csvFilePath);

		Map<String, String> cmRecordMap = null;
		String fileName = FilenameUtils.getBaseName(csvFilePath);

		try {
			CsvReader csvReader = new CsvReader(csvFilePath);

			csvReader.readHeaders();

			// Get CSV header array
			String[] csvHeaderArray = csvReader.getHeaders();

			// Ignore the headers when start to read records
			while (csvReader.readRecord()) {
				String[] csvRecordValueArray = csvReader.getValues();
				int csvRecordValueArrayLength = csvRecordValueArray.length;

				StringBuilder lineRecordStringBuilder = new StringBuilder();

				for (int index = 0; index < csvRecordValueArrayLength; index++) {
					lineRecordStringBuilder.append(csvRecordValueArray[index]);
				}

				// Continue next cycle when it's empty record
				if (StringUtils.trimToEmpty(lineRecordStringBuilder.toString()).equals("")) {
					break;
				}

				int csvHeaderArrayLength = csvHeaderArray.length;
				cmRecordMap = new ConcurrentHashMap<String, String>();

				for (int index = 0; index < csvHeaderArrayLength; index++) {
					String fieldName = csvHeaderArray[index];

					String fieldValue = StringUtils.trimToEmpty(CommonTool.convertReserveSymbel(csvReader.get(fieldName), false));
					logger.info("Field Name = Value: " + fieldName + " = " + fieldValue);

					// Put fieldName(Key) and fieldValue(Value) into Map as one record
					cmRecordMap.put(fieldName, fieldValue);
				}
			}

			csvReader.close();

			// Store entire CSV records into Map(Key: fileName; Value: recordMap)
			cmMap.put(fileName, cmRecordMap);
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		return cmMap;
	}
}


上一篇:TOPO DN 解析


下一篇:内存数据库 HSQLDB