import Csv from File - Java File Path IO

Java examples for File Path IO:CSV File

Description

import Csv from File

Demo Code


//package com.java2s;
import java.io.BufferedReader;

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));
        List<String[]> lineList = new ArrayList<String[]>();
        BufferedReader br = null;
        try {/*  w  ww  . java  2 s .c  om*/
            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 {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return lineList;
    }
}

Related Tutorials