Converting CSV File Into 2D Array

前端 未结 5 1659
南笙
南笙 2021-01-19 03:45

I have an example of my idea in a 1d array. It will only output the columns. My idea is to use a 2d array to select the row and column. Here my code:

String          


        
相关标签:
5条回答
  • 2021-01-19 04:21

    It's better to use an ArrayList of 1D String arrays, instead of a 2D String array.

    String fName = "c:\\csv\\myfile.csv";
    String thisLine;
    FileInputStream fis = new FileInputStream(fName);
    DataInputStream myInput = new DataInputStream(fis);
    ArrayList<String[]> strar = new ArrayList<String[]>();
    
    while ((thisLine = myInput.readLine()) != null) {
        strar.add(thisLine.split(";"));
    }
    

    Note: Also you need to handle the IOException here.

    0 讨论(0)
  • 2021-01-19 04:26

    I would use a dynamic structure to read all lines. Also you should use a try-resource block to close the streams automatically.

    public static String[][] readCSV(String path) throws FileNotFoundException, IOException {
        try (FileReader fr = new FileReader(path);
                BufferedReader br = new BufferedReader(fr)) {
            Collection<String[]> lines = new ArrayList<>();
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                lines.add(line.split(";"));
            }
            return lines.toArray(new String[lines.size()][]);
        }
    }
    
    0 讨论(0)
  • 2021-01-19 04:29

    I would just add the split result (String[]) to a List then if you really want it as a 2d array then convert it after the fact.

    List<String[]> lines = new ArrayList<String[]>();
    while ((thisLine = myInput.readLine()) != null) {
         lines.add(thisLine.split(";"));
    }
    
    // convert our list to a String array.
    String[][] array = new String[lines.size()][0];
    lines.toArray(array);
    
    0 讨论(0)
  • 2021-01-19 04:32

    This is my implemented code:

    String fileName = "myfile.csv";
    List<List<String>> list = new ArrayList<List<String>>();
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String line = br.readLine();
    String[] headers = line.split(";");
    for(String header: headers) {
        List<String> subList = new ArrayList<String>();
        subList.add(header);
        list.add(subList);
    }
    while((line = br.readLine()) != null) {
        String[] elems = line.split(";");
        for(int i = 0; i < elems.length; i++) {
            list.get(i).add(elems[i]);
        }
    }
    br.close();
    int rows = list.size();
    int cols = list.get(0).size();
    String[][] array2D = new String[rows][cols];
    for(int row = 0; row < rows; row++) {
        for(int col = 0; col < cols; col++) {
            array2D[row][col] = list.get(row).get(col);
        }
    }
    

    The array2D is your wants.

    0 讨论(0)
  • 2021-01-19 04:44

    First thing we don't know how many lines are there in the csv file. So it's impossible to determine the length of the 2d array. We have to increment the size of the array according to this case. But, normally it's impossible to re-size the array with java. So we create new array and copy contents of source array when we need to re-size the array.

    Solution for you:

    int i = 0;//line count of csv
    String[][] data = new String[0][];//csv data line count=0 initially
    while ((thisLine = myInput.readLine()) != null) {
        ++i;//increment the line count when new line found
    
        String[][] newdata = new String[i][2];//create new array for data
    
        String strar[] = thisLine.split(";");//get contents of line as an array
        newdata[i - 1] = strar;//add new line to the array
    
        System.arraycopy(data, 0, newdata, 0, i - 1);//copy previously read values to new array
        data = newdata;//set new array as csv data
    }
    

    Create test to view csv data:

    for (String[] strings : data) {
        for (String string : strings) {
            System.out.print("\t" + string);
        }
        System.out.println();
    }
    

    Output:

    Id  name
    E1  Tim
    A1  Tom
    
    0 讨论(0)
提交回复
热议问题