集册 Java实例教程 从文件导入Csv

从文件导入Csv

欢马劈雪     最近更新时间:2020-01-02 10:19:05

528
从文件导入Csv


//package com.nowjava;

import java.io.BufferedReader;

/*
来 自*
 nowjava - 时代Java
*/

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;


import java.io.IOException;

import java.io.InputStreamReader;


import java.util.ArrayList;

import java.util.List;


public class Main {


    public static List<String[]> importCsv(String filename)

            throws FileNotFoundException {

        File file = new File(filename);

        DataInputStream in = new DataInputStream(new FileInputStream(file));/*N o w J a v a . c o m - 时代Java*/

        List<String[]> lineList = new ArrayList<String[]>();

        BufferedReader br = null;

        try {

            br = new BufferedReader(new InputStreamReader(in, "GBK"));

            String line = "";

            String csvSplitBy = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";

            while ((line = br.readLine()) != null) {

                //               String [] major = line.split(csvSplitBy);  

                lineList.add(line.split(csvSplitBy));

                //               System.out.println("major"+major[5]);   


            }

        } catch (Exception e) {

        } finally {
展开阅读全文