java读取csv文件


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CsvUtil {

	private InputStreamReader fr = null;
	private BufferedReader br = null;

	public CsvUtil(String f) throws IOException {
		fr = new InputStreamReader(new FileInputStream(f));
	}

	/**
	 * 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中
	 */
	public List> readCSVFile() throws IOException {
		br = new BufferedReader(fr);
		String rec = null;// 一行
		String str;// 一个单元格
		List> listFile = new ArrayList>();
		try {
			// 读取一行
			while ((rec = br.readLine()) != null) {
				int index=0;
				Pattern pCells = Pattern
						.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
				Matcher mCells = pCells.matcher(rec);
				List cells = new ArrayList();// 每行记录一个list
				// 读取每个单元格
				while (mCells.find()) {
					str = mCells.group();
					str = str.replaceAll(
							"(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
					str = str.replaceAll("(?sm)(\"(\"))", "$2");
					cells.add(str);
					index = mCells.end();
				}
				cells.add(rec.substring(index));
				//if(rec.substring(index)!=null&&)
				listFile.add(cells);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fr != null) {
				fr.close();
			}
			if (br != null) {
				br.close();
			}
		}
		return listFile;
	}

	public static void main(String[] args) throws Throwable {
		CsvUtil test = new CsvUtil("F:\\users.csv");
		List> csvList = test.readCSVFile();
	}

}

你可能感兴趣的:(java与net,windows,python,开发语言)