简体   繁体   English

将csv文件的内容复制/写入数组

[英]copy / write contents of csv file to an array

I am currently learning java and need to know the easiest way to copy the contents of a csv file into an array and again copy the contents of an array into a csv file. 我目前正在学习java,需要知道将csv文件的内容复制到数组中的最简单方法,并再次将数组的内容复制到csv文件中。 So currently I have a CSV file employee.csv that contains the following: 所以目前我有一个CSV文件employee.csv,其中包含以下内容:

Name,Company,Experience.

Now I need to retrieve employees with experience greater than 2 and put them in another array that contains Name, Experience. 现在,我需要检索经验大于2的员工,并将它们放在包含Name,Experience的另一个数组中。

I want to write this array to another .csv file called results.csv 我想将此数组写入另一个名为results.csv的.csv文件中

What would be the best way to accomplish this? 实现这一目标的最佳方法是什么?

Instead of using some library to do this task for you, I think you should try to write the code yourself. 我认为您应该尝试自己编写代码,而不是使用某些库来为您完成此任务。 Parsing a CSV file is a good exercise for someone "currently learning java." 解析CSV文件对于“正在学习java”的人来说是一个很好的练习。

Below is a class to hold the Employees and method to parse the file. 下面是一个用于保存Employees和方法来解析文件的类。 There should probably be more error checking code. 应该有更多错误检查代码。 If the file has a line without two commas in it, the program will crash. 如果文件中有一行没有两个逗号,则程序将崩溃。 I didn't handle the experience value check for you. 我没有为您处理经验值检查。 You can either check that as you create the employees and only add the new object to the array if they meet the experience requirement, or you could check that value before writing to the file. 您可以在创建员工时检查它,只有在满足经验要求时才将新对象添加到阵列,或者可以在写入文件之前检查该值。 To simplify output to a file, I wrote a CSVString method in Employee. 为了简化输出到文件,我在Employee中编写了一个CSVString方法。

public class Employee {
    private String name;
    private String company;
    private int experience;

    public Employee(String name, String company, int experience) {
        this.name = name;
        this.company = company;
        this.experience = experience;
    }

    public Employee(String name, String company, String exp) {
        this.name = name;
        this.company = company;
        try {
            experience = Integer.parseInt(exp.trim());
        }
        catch(NumberFormatException utoh) {
            System.out.println("Failed to read experience for " + name + 
                    "\nCannot conver to integer: " + exp);
        }
    }

    public String toCSVString() {
       return name + "," + company + "," + experience;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public int getExperience() {
        return experience;
    }

    public void setExperience(int experience) {
        this.experience = experience;
    }
}

Now for reading in the list from the file: 现在从文件中读取列表:

public static ArrayList<Employee> readEmployeeFile(File csvFile) {
   ArrayList<Employee> list = new ArrayList<>();
   Employee joe;
   try {
       Scanner in = new Scanner(csvFile);
       String line;
       while(in.hasNextLine()) {
            line = in.nextLine().trim();
            String[] col = line.split(",");
            joe = new Employee(col[0],col[1],col[2]);
            list.add(joe);
       }
       in.close();
    }
    catch(IOException ug) {
        System.out.println("Error reading line: " + line);
        System.out.println(ug);
    }
    return list;
}

You can have a look here to find ways to read from and write in a csv file. 您可以在这里查看如何读取和写入csv文件。

You just need to check for the Experience after reading from the employee.csv and before writing to your result.csv file. 您只需在从employee.csv读取之后和写入result.csv文件之前检查Experience

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

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