简体   繁体   中英

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. So currently I have a CSV file employee.csv that contains the following:

Name,Company,Experience.

Now I need to retrieve employees with experience greater than 2 and put them in another array that contains Name, Experience.

I want to write this array to another .csv file called results.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."

Below is a class to hold the Employees and method to parse the file. 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.

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.

You just need to check for the Experience after reading from the employee.csv and before writing to your result.csv file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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