2

Hi while saving object by using openCsv the data getting stored in a single line and if i try to add more than one object the data getting deleted of the previous object. please if anyone knows the solution answer it here.

public final Logger log=Logger.getLogger("Error");
    public void beanToCsvExport(final T object, final String filePath) {
        if(object == null) {
            log.warning("Initialization of object failed");
        }else {
            try {
                Writer writer = new FileWriter(filePath);
                StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
                beanToCsv.write(object);
                writer.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } catch (CsvDataTypeMismatchException e) {
                System.out.println(e.getMessage());
            } catch (CsvRequiredFieldEmptyException e) {
                System.out.println(e.getMessage());
            }
        }
    }
Pluto_rider
  • 127
  • 1
  • 8

1 Answers1

3

When you try to write more than one object the last one is overwritten because the FileWriter you are passing to StatefulBeanToCsv is not in append mode. You should specify the append mode in the FileWriter constructor.

Writer writer = new FileWriter(filePath, true);
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
beanToCsv.write(object);
writer.close();

Edit: This should solve multiple columns headers and column header not breaking line. It works for me.

public <T> void beanToCsvExport(final List<T> object, final String filePath) {
    if (object == null) {
        System.out.println("Initialization of object failed");
    } else {
        try {
            cleanFile(filePath);
            FileWriter writer = new FileWriter(filePath, true);
            StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
            beanToCsv.write(object);
            writer.close();
        } catch (IOException | CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) {
            System.out.println(e.getMessage());
        }
    }
}

The function that resets the file:

public void cleanFile(String path) throws IOException {
    FileWriter writer = new FileWriter(path); //Open as non-append to delete all data.
    writer.write("");
    writer.close();
}

To test the functions:

public void employeesToCsv() {
    List<Employee> list = Arrays.asList(new Employee("101", "sunny"), new Employee("102", "Andrew"));
    beanToCsvExport(list, "file.csv");
}

The achieved output:

"eId","eName"
"101","sunny"
"102","Andrew"
PauMAVA
  • 1,163
  • 14
  • 23
  • It's working thank you but it is not switching the line. and column header is appearing as many times as i'm adding object. – Pluto_rider Apr 30 '20 at 06:49
  • Try adding a line break as: `writer.write("\n");` after writing the object. – PauMAVA Apr 30 '20 at 06:51
  • output: "eId","eName","101","sunny""eId","eName""102","Andrew" – Pluto_rider Apr 30 '20 at 06:54
  • If you are calling `beanToCsv(object)` for every object it will write the column header every time. Instead of passing an instance of type T to `beanToCsv` pass a `List` containing all the objects you want to write. – PauMAVA Apr 30 '20 at 06:58
  • But still i need to iterate that right? it will be object again – Pluto_rider Apr 30 '20 at 07:01
  • Can you share the class of the object (type T) that you are passing to `beanToCsv`? Then I will test and post an example. :) – PauMAVA Apr 30 '20 at 07:03
  • List is working one header is created but not changing the line. it is a simple employ class with id and name. – Pluto_rider Apr 30 '20 at 07:06
  • I have edited the answer with the code I have tested. – PauMAVA Apr 30 '20 at 07:25
  • 1
    I've copy pasted the same code can you tell me why it's not working for me? or please send the file at kandelamanikanta@gmail.com thanks a lot for your efforts – Pluto_rider Apr 30 '20 at 09:35