How to serialize object to CSV file?

前端 未结 8 1304
执念已碎
执念已碎 2020-11-27 17:14

I want to write a Object into CSV file. For XML we have XStream like this
So if i want to convert object to CSV do we have any such library ?

EDIT: I w

8条回答
  •  鱼传尺愫
    2020-11-27 17:57

    I wrote a simple class that uses OpenCSV and has two static public methods.

    static public File toCSVFile(Object object, String path, String name) {
        File pathFile = new File(path);
        pathFile.mkdirs();
        File returnFile = new File(path + name);
        try {
    
            CSVWriter writer = new CSVWriter(new FileWriter(returnFile));
            writer.writeNext(new String[]{"Member Name in Code", "Stored Value", "Type of Value"});
            for (Field field : object.getClass().getDeclaredFields()) {
                writer.writeNext(new String[]{field.getName(), field.get(object).toString(), field.getType().getName()});
            }
            writer.flush();
            writer.close();
            return returnFile;
        } catch (IOException e) {
            Log.e("EasyStorage", "Easy Storage toCSVFile failed.", e);
            return null;
        } catch (IllegalAccessException e) {
            Log.e("EasyStorage", "Easy Storage toCSVFile failed.", e);
            return null;
        }
    }
    
    static public void fromCSVFile(Object object, File file) {
        try {
            CSVReader reader = new CSVReader(new FileReader(file));
            String[] nextLine = reader.readNext(); // Ignore the first line.
            while ((nextLine = reader.readNext()) != null) {
                if (nextLine.length >= 2) {
                    try {
                        Field field = object.getClass().getDeclaredField(nextLine[0]);
                        Class rClass = field.getType();
                        if (rClass == String.class) {
                            field.set(object, nextLine[1]);
                        } else if (rClass == int.class) {
                            field.set(object, Integer.parseInt(nextLine[1]));
                        } else if (rClass == boolean.class) {
                            field.set(object, Boolean.parseBoolean(nextLine[1]));
                        } else if (rClass == float.class) {
                            field.set(object, Float.parseFloat(nextLine[1]));
                        } else if (rClass == long.class) {
                            field.set(object, Long.parseLong(nextLine[1]));
                        } else if (rClass == short.class) {
                            field.set(object, Short.parseShort(nextLine[1]));
                        } else if (rClass == double.class) {
                            field.set(object, Double.parseDouble(nextLine[1]));
                        } else if (rClass == byte.class) {
                            field.set(object, Byte.parseByte(nextLine[1]));
                        } else if (rClass == char.class) {
                            field.set(object, nextLine[1].charAt(0));
                        } else {
                            Log.e("EasyStorage", "Easy Storage doesn't yet support extracting " + rClass.getSimpleName() + " from CSV files.");
                        }
                    } catch (NoSuchFieldException e) {
                        Log.e("EasyStorage", "Easy Storage fromCSVFile failed.", e);
                    } catch (IllegalAccessException e) {
                        Log.e("EasyStorage", "Easy Storage fromCSVFile failed.", e);
    
                    }
                } // Close if (nextLine.length >= 2)
            } // Close while ((nextLine = reader.readNext()) != null)
        } catch (FileNotFoundException e) {
            Log.e("EasyStorage", "Easy Storage fromCSVFile failed.", e);
        } catch (IOException e) {
            Log.e("EasyStorage", "Easy Storage fromCSVFile failed.", e);
        } catch (IllegalArgumentException e) {
            Log.e("EasyStorage", "Easy Storage fromCSVFile failed.", e);
        }
    }
    

    I think with some simple recursion these methods could be modified to handle any Java object, but for me this was adequate.

提交回复
热议问题