0

Taking this example:

public class Dog {
    private String name;
    private Owner owner;
}

public class Owner {
    private String name;
    private int age;
    private String address;
}
  • How I can add multiple Dog objects to a CSV?
  • How would they look like in the CSV with their owner object inside it?
  • How are Java Beans useful in this situation?

1 Answers1

1

There are a few approaches, depending on what you want to accomplish. You could write a function that writes a separate CSV for the owners and dogs, where they could reference each other using a key, like a sql primary key. Or you could simply inline the information into one, maybe with columns like:

dog_name, owner_name, owner_age, owner_address

What Java Beans can allow you to do is what is called serialization, where you let Java handle how to read and write objects from disk. Normally, you'd run into issues where Java does not know how to serialize objects that are nested inside each other, but by using Beans you can still accomplish this. You could do this by making Dog implement java.io.Serializable. Check out this guide from TutorialsPoint. More info about beans here.