当前位置: 首页 > 图灵资讯 > 技术篇> 使用 Java 泛型实现 CSV 到对象的转换

使用 Java 泛型实现 CSV 到对象的转换

来源:图灵教育
时间:2025-09-15 17:42:36

使用 java 泛型实现 csv 到对象的转换

本文介绍了如何使用 Java 创建一个通用的泛型 CSV 文件转换器,将 CSV 文件中的数据动态地转换为不同类型的数据 Java 对象,例如 Cat 和 Dog。 避免使用泛型为每种对象类型编写重复代码,提高代码的可重用性和可维护性。 同时,建议使用现有的 CSV 为了简化开发,提高代码的强度,分析库。

使用 Java 一般的泛型构造 CSV 转换器

在 Java 在开发过程中,往往需要进行 CSV 文件中的数据转换为 Java 对象。 如果为每个对象类型编写一种特定的转换方法,则代码冗余且难以维护。 利用 Java 泛型,可以创建一个通用的 CSV 转换器可以动态移动 CSV 将数据转换为不同类型的数据 Java 对象。

1. 创建泛型 CSV 工具类

首先,创建一个泛型类型 CsvUtils,使用类型参数 T 表示要转换成 Java 对象类型。

public class CsvUtils<T> {

    public List<T> read(final String fileName, final Class<T> clazz) throws IOException {
        List<T> 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<T> 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;
        }
    }
}

2. 使用泛型 CSV 工具类

现在,可以用了 CsvUtils 类将 CSV 文件转换为 Cat 或 Dog 对象列表。

立即学习“Java免费学习笔记(深入);

public class Example {

    public void doSomeStuffWithMyDogs() throws IOException {
        CsvUtils<Dog> csvUtils = new CsvUtils<>();
        List<Dog> 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<Cat> csvUtils = new CsvUtils<>();
        List<Cat> myCats = csvUtils.read("MyCats_V1.csv", Cat.class);

        // do something else with myCats
        for (Cat cat : myCats) {
            System.out.println(cat);
        }
    }
}

3. 注意事项和改进
  • 异常处理: 在实际应用中,需要更加完善的异常处理机制,如记录错误日志、提供友好的错误提示等。
  • 类型转换: createObject 该方法需要根据实际情况添加更多的类型转换逻辑,如日期、布尔值等。
  • CSV 解析库: 不建议手动分析 CSV 建议使用现有的文件, CSV 例如,分析库 Apache Commons CSV, OpenCSV, SimpleFlatMapper CSV parser, jackson-dataformat-csv, uniVocity-parsers, deephaven-csv 等。 这些图书馆提供了更强大的功能和更好的性能,可以简化开发,提高代码的强度。
  • 对象创建: createObject 该方法使用反射来创建对象,这可能会影响性能。 可以考虑使用工厂模式或构建器模式来创建对象。
  • 字段映射: 当前的实现假设 CSV 文件的顺序和顺序 Java 对象的字段顺序是一致的。 如果不一致,则需要添加字段映射的逻辑。
4. 使用 Apache Commons CSV 示例

以下示例显示了如何使用它 Apache Commons CSV 库来解析 CSV 文件。

首先,添加 Apache Commons CSV 的依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.9.0</version>
</dependency>

然后,修改 CsvUtils 类:

import org.apache.commons.csv.*;

public class CsvUtils<T> {

    public List<T> read(final String fileName, final Class<T> clazz) throws IOException {
        List<T> 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<T> 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) 获取指定列的值。

5. 总结

使用 Java 泛型可以创建通用 CSV 避免为每种对象类型编写重复代码。 建议使用现有代码,以简化开发,提高代码的强度 CSV 解析库。 同时,要注意异常处理、类型转换、对象创建和字段映射。

以上就是使用 Java 泛型实现 CSV 更多关于图灵教育的其他相关文章,请关注对象转换的详细内容!