简体   繁体   English

将.csv文件读入java中的数组

[英]reading a .csv file into an array in java

I have a class called EmpQuery that I am trying to create an array of objects for my Employee class to hold data that comes from a employeedatabase.csv file.我有一个名为 EmpQuery 的类,我试图为我的 Employee 类创建一个对象数组,以保存来自employeedatabase.csv 文件的数据。 The database appears like what is shown below.数据库如下所示。 I need to use a stream-processing-algorithm..我需要使用流处理算法..

Loop till EOF{
read in 1 record
Deal with that record completly
}

EX.前任。

Employee ID,Full Name,Department,Start Date,Earnings
EMP001,Adele M. Fulton,Engineering,10/28/2008,"$104,000.00"
EMP002,Ali T. Herman,Engineering,2/27/2012,"$337,522.00"
EMP003,Alika C. Crawford,Engineering,6/2/2009,"$144,000.00"

So far i just have this much set up public class EmployeeDB {到目前为止,我只设置了这么多公共类 EmployeeDB {

    private static String[] empID = new String[300];
    private static String[] empName = new String[300];
    private static String[] department = new String[300];
    private static String[] startDate = new String[300];
    private static String [] earnings = new String[300];
    private static String [] empDataBase = new String[300];
    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        fillDataArray();
    }

   public class employee{
      String empID;
      String empName;
      String department;
      String startDate;
      int earnings;

   public employee(String ID,String Name,String dept,String sDate,int earn){

          empName = Name;
          empID = ID;
          department = dept;
          startDate = sDate;
          earnings = earn;
   }      
    public employee( String ID, String Name)  {

          empName = Name;
          empID = ID;
          department = "";
          startDate = "";
          earnings = 0;

   } 
    public employee(){

           empName = "";
           empID = "";
           department = "";
           startDate = "";
           earnings = 0;

    }

   }
    private static String[] fillDataArray() throws FileNotFoundException {
        File DatabaseFile = new File("EmpDB_lab7.csv");
        Scanner inputFile = new Scanner(DatabaseFile);
        String InputLine;
        String [] empDBTemp = null;
        int i=0;

        while (inputFile.hasNextLine()) {
            InputLine = inputFile.nextLine();
            empDBTemp = InputLine.split("-");
            empID[i] = empDBTemp[1];
            empName[i] = empDBTemp[2];
            department[i] = empDBTemp[3];
            startDate[i] = empDBTemp[4];
            earnings[i] = empDBTemp[5];
        }



    return empDBTemp;

    }
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at employeedb.EmployeeDB.fillDataArray(EmployeeDB.java:76)
    at employeedb.EmployeeDB.main(EmployeeDB.java:28)
Java Result: 1

CSV parsing is harder than it appears, so you should use a pre-existing library, such as Super CSV CSV 解析比看起来更难,因此您应该使用预先存在的库,例如Super CSV

Your CSV column names should match your bean field names (or skip the header altogether), and earnings could be a String, else you'll want to use a CellProcessor to parse it.您的 CSV 列名称应与您的 bean 字段名称匹配(或完全跳过标题),并且收入可以是字符串,否则您将需要使用 CellProcessor 来解析它。

Reading stuff into an array and stream processing are contradictory.将内容读入数组和流处理是矛盾的。

ICsvBeanReader csvReader = new CsvBeanReader(new FileReader("employeedatabase.csv"), CsvPreference.STANDARD_PREFERENCE);

String[] header = csvReader.getHeader(false);

EmployeeDB employeeDB;

while((employeeDB = csvReader.read(EmployeeDB.class, header)) != null) {
    System.out.println(employeeDB.getEmpName());
}

csvReader.close();

And

public class EmployeeDB {

    private String empID;
    private String empName;
    private String department;
    private String startDate;
    private String earnings;

    ...getters/setters...
}

I changed the header to:我将标题更改为:

EmpID,EmpName,Department,StartDate,Earnings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM