简体   繁体   中英

Java converting csv file to java object(relevant to generic type)

I try to write program which provide converting csv file to java object.I have a lot of csv files and classes.I don't want to write different method for each csv-class pair.I wrote one method but I didn't like it(Because I am new to generic type) please give an advise for efficient solution.Thanks for your attention.

Main :

CsvParser<Person> parser =  new CsvParser();
LinkedList<Person> a = parser.readCsv(chooser.getSelectedFile(),Person.class);

CsvParser :

public class CsvParser<T> {

public LinkedList<T> readCsv(File file, Class<T> t) {
    String[] columnNames = null;
    LinkedList<T> result = new LinkedList<>();
    try {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            boolean isColumnNames = true;
            while ((line = reader.readLine()) != null) {
                String[] attributes = line.split("\\|");
                if (isColumnNames) {
                    columnNames = attributes;
                    isColumnNames = false;
                } else {
                    T newInstance = t.newInstance();
                    Field[] fields = t.getDeclaredFields();
                    for (Field field : fields) {
                        for (int i = 0; i < columnNames.length; i++) {
                            if (field.getName().equalsIgnoreCase(columnNames[i])) {
                                field.setAccessible(true);
                                field.set(newInstance, attribute[i]);
                            }
                        }
                    }
                    result.add(newInstance);
                }
            }
        } catch (InstantiationException ex) {
            Logger.getLogger(CsvParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CsvParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CsvParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CsvParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
} 
}

csv file is like that : (separator is "|")

BusinessEntityID|PersonType|Title|FirstName|MiddleName|LastName|ModifiedDate    
1|EM||Ken|J|Sánchez|1999-02-08  00:00:00
2|EM||Terri|Lee|Duffy|1998-02-24    00:00:00
3|EM||Roberto||Tamburello|1997-12-05    00:00:00
4|EM||Rob||Walters|1997-12-29   00:00:00

getdeclaredfields parameters cannot be case insensitive.Person class has an attribute businessEntitiyId but csv has BusinessEntityID So i must write the part of code like that :(

Field[] fields = t.getDeclaredFields();
for (Field field : fields) {
    for (int i = 0; i < columnNames.length; i++) {
         if (field.getName().equalsIgnoreCase(columnNames[i])) {
             field.setAccessible(true);
             field.set(newInstance, attribute[i]);
         }
    }
}

我猜您想将字段设置为您刚刚读取的行的值,而不是列的名称:

field.set(newInstance, attributes[i]);

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