1

Given two Classes

public class Inventory {
    private List<InventoryLine> lines;
}

and

public class InventoryLine {
    private String itemName;
    private int quantity;
    private String description;
}

I would like to print the following CSV file (using ',' as the delimiter but for visibility's sake I used spaces here):

Name    Quantity    Description
Book    4           A book
Watch   0           Gold watch
Phone   8           iPhone 8

Using the Conversion annotation and a class implementing the Conversion interface provided would it be possible to create the above CSV (by printing the Inventory class)? I see that they have provided examples with a List but they merge them into one column using a delimiter. I don't see any examples with this case.

2 Answers 2

1

Author of the library here. The parser doesn't handle a hierarchical structure of classes. You can only write the list itself:

new CsvRoutines().writeAll(inventory.getLines(), InventoryLine.class, new File("/path/to/your.csv"), "UTF-8");

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

If you consider using other library.

try this one

maven dependency

   <dependency>
        <groupId>io.github.francisfernandez28</groupId>
        <artifactId>libraries</artifactId>
        <version>0.2.5-SNAPSHOT</version>
    </dependency>

Usage:

String csvString = CSVUtils.generateCSVStringFromListOfModel(inventory.getLines(),",");

then write

csvString

into your file.

1 Comment

This doesn't solve his problem. He wants to write the instance of Inventory itself and let the library take care of any internally stored collections of objects. In his case there's only one, but it could very well be a complex hierarchy not suitable for the CSV format. My suggestion is pretty much the same as yours: write the result of inventory.getLines()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.