-2

I am implementing csv file listener using watchservice. My requirement is : Get csv files from the registered directory with java watch service. I am struggling to process the received files and stored data into the database. the problem here is - CSV file format is not predefined, file can have any number of columns and different headers. I have referred many sites but not found any solution. please help.

2
  • Can you share the code you have tried so far? Commented Feb 27, 2016 at 13:45
  • 1
    if the file formats are completely arbitrary, you can't meaningfully process it with a generic code. One thing you can do is parse the file manually line by line and split on , and do it in an infinite loop till you reach the end-of-file. Commented Feb 27, 2016 at 13:59

1 Answer 1

1

this sample code helps you to process your files:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    private static String HEADER_DELIMITER = ",";
    private static String DATA_DELIMITER = ",";

    public static void main(String[] args) {
        /**
         * for each file read lines and then ...
         */
        String headerLine = "id,name,family";
        String[] otherLines = { "1,A,B", "2,C,D" };
        List<Student> students = new ArrayList<Student>();
        String[] titles = headerLine.split(HEADER_DELIMITER);
        for (String line : otherLines) {
            String[] cells = line.split(DATA_DELIMITER);
            Student student = new Student();
            int i = 0;
            for (String cell : cells) {
                student.add(titles[i], cell);
                i++;
            }
            students.add(student);
        }
        System.out.println(students);
        /*
         * output:
         * [Student [data={id=1, family=B, name=A}], Student [data={id=2, family=D, name=C}]]
         */

        /**
         * save students in your table.
         */
    }

    static class Student {
        Map<String, String> data = new HashMap<String, String>();

        public void add(String key, String value) {
            data.put(key, value);
        }

        @Override
        public String toString() {
            return "Student [data=" + data + "]";
        }
    }
}

for saving the result in your data base, it depends on your decision about your data model. for example if these csv files are common in most columns and are different in a few columns I suggest this data model:

you should have a table (for example Student) with common columns (id, name, family) and another table with only 3 columns (studentForeignKey, key, value) and store other extra columns in this table. for example (id=1, key=activity, value=TA) and it means that the student with id = 1 has TA activity.

I hope this answer could help you

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.