[英]How to add an object that its class has a different class member to an CSV?
Taking this example:举个例子:
public class Dog {
private String name;
private Owner owner;
}
public class Owner {
private String name;
private int age;
private String address;
}
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 .
您可以编写一个 function ,它为主人和狗编写一个单独的 CSV ,它们可以使用一个键相互引用,例如sql 主键。 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. Java Beans 可以让你做的就是所谓的序列化,你让 Java 处理如何从磁盘读取和写入对象。 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.
通常,您会遇到 Java 不知道如何序列化相互嵌套的对象的问题,但使用 Beans 您仍然可以完成此操作。 You could do this by making Dog
implement java.io.Serializable
.您可以通过使 Dog
implement java.io.Serializable
来做到这一点。 Check out this guide from TutorialsPoint.从 TutorialsPoint 查看本指南。 More info about beans here .
有关豆类的更多信息在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.