本文介绍了如何使用 Java 泛型创建一个通用的 CSV 文件转换器,将 CSV 文件中的数据动态地转换为不同类型的 Java 对象,例如 Cat 和 Dog。 通过使用泛型,避免了为每种对象类型编写重复代码,提高了代码的可重用性和可维护性。 同时,推荐使用现有的 CSV 解析库,以简化开发并提高代码的健壮性。
在 Java 开发中,经常需要将 CSV 文件中的数据转换为 Java 对象。 如果针对每种对象类型都编写一个特定的转换方法,会导致代码冗余且难以维护。 利用 Java 泛型,可以创建一个通用的 CSV 转换器,能够动态地将 CSV 数据转换为不同类型的 Java 对象。
首先,创建一个泛型类 CsvUtils,使用类型参数 T 表示要转换成的 Java 对象类型。
public class CsvUtils{ public List read(final String fileName, final Class clazz) throws IOException { List objectList = new ArrayList<>(); Path pathToFile = Paths.get(fileName); try (BufferedReader br = Files.newBufferedReader(pathToFile)) { String line = br.readLine(); // Skip header line while ((line = br.readLine()) != null) { String[] attributes = line.split(","); T obj = createObject(attributes, clazz); if (obj != null) { objectList.add(obj); } } } catch (IOException e) { e.printStackTrace(); } return objectList; } private T createObject(String[] attributes, Class clazz) { try { T obj = clazz.getDeclaredConstructor().newInstance(); // Assuming the attributes order matches the fields order in the class Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length && i < attributes.length; i++) { fields[i].setAccessible(true); // Allow access to private fields // Attempt to convert the string value to the field's type try { if (fields[i].getType() == int.class || fields[i].getType() == Integer.class) { fields[i].set(obj, Integer.parseInt(attributes[i])); } else if (fields[i].getType() == String.class) { fields[i].set(obj, attributes[i]); } // Add more type conversions as needed } catch (NumberFormatException e) { System.err.println("Error converting value for field " + fields[i].getName() + ": " + e.getMessage()); } } return obj; } catch (Exception e) { System.err.println("Error creating object of type " + clazz.getName() + ": " + e.getMessage()); return null; } } }
现在,可以使用 CsvUtils 类将 CSV 文件转换为 Cat 或 Dog 对象列表。
public class Example {
public void doSomeStuffWithMyDogs() throws IOException {
CsvUtils csvUtils = new CsvUtils<>();
List myDogs = csvUtils.read("MyDogs_V1.csv", Dog.class);
// do something else with myDogs
for (Dog dog : myDogs) {
System.out.println(dog);
}
}
public void doSomeStuffWithMyCats() throws IOException {
CsvUtils csvUtils = new CsvUtils<>();
List myCats = csvUtils.read("MyCats_V1.csv", Cat.class);
// do something else with myCats
for (Cat cat : myCats) {
System.out.println(cat);
}
}
} 以下示例展示了如何使用 Apache Commons CSV 库来解析 CSV
文件。
首先,添加 Apache Commons CSV 的依赖:
org.apache.commons commons-csv1.9.0
然后,修改 CsvUtils 类:
import org.apache.commons.csv.*; public class CsvUtils{ public List read(final String fileName, final Class clazz) throws IOException { List objectList = new ArrayList<>(); Path pathToFile = Paths.get(fileName); try (BufferedReader br = Files.newBufferedReader(pathToFile); CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(br)) { for (CSVRecord record : parser) { T obj = createObject(record, clazz); if (obj != null) { objectList.add(obj); } } } catch (IOException e) { e.printStackTrace(); } return objectList; } private T createObject(CSVRecord record, Class clazz) { try { T obj = clazz.getDeclaredConstructor().newInstance(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String columnName = field.getName(); // Assuming column name matches field name String value = record.get(columnName); try { if (field.getType() == int.class || field.getType() == Integer.class) { field.set(obj, Integer.parseInt(value)); } else if (field.getType() == String.class) { field.set(obj, value); } // Add more type conversions as needed } catch (NumberFormatException e) { System.err.println("Error converting value for field " + field.getName() + ": " + e.getMessage()); } catch (IllegalArgumentException e) { System.err.println("Column not found: " + columnName); } } return obj; } catch (Exception e) { System.err.println("Error creating object of type " + clazz.getName() + ": " + e.getMessage()); return null; } } }
这个示例使用了 CSVFormat.DEFAULT.withFirstRecordAsHeader() 来指定 CSV 文件的第一行是标题行,并使用 record.get(columnName) 来获取指定列的值。
使用 Java 泛型可以创建通用的 CSV 转换器,避免为每种对象类型编写重复代码。 为了简化开发并提高代码的健壮性,推荐使用现有的 CSV 解析库。 同时,需要注意异常处理、类型转换、对象创建和字段映射等方面的问题。