如何从列表中包含化合物 class 的列表中生成 Java 中的 CSV 文件
How to generate CSV file in Java from the List where List has compound class
我关注 Java class 并列为:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
我想从列表中生成 CSV 文件。如果没有复合 class 胡言乱语,我可以很容易地做到这一点。你能建议我该怎么做吗?我尝试了 opencsv 并申请这样做。 Opencsv 适用于非化合物 class。我可能做错了什么。所以代码示例会有所帮助。
CSV="Comma Separated Values"。
您可以自己创建一个 csv 文件。您可以在文件中写入以逗号(或分号)分隔的值。这些值是一行的单元格。一行结束了,你写一个行分隔符。
hier 是如何以 csv 格式写入行的简单基本示例:
public static final char CSV_SEPARATOR = ';'; // it could be a comma or a semi colon
try (BufferedWriter writer = new BufferedWriter(new FileWriter("my_file.csv"))) {
personList.forEach(person -> {
writer.append(person.getName()).append(CSV_SEPARATOR)
.append(person.getAge()).append(CSV_SEPARATOR)
.append(persone.getAddress().getStreet()).append(CSV_SEPARATOR)
.append(persone.getAddress().getpostCode()).append(CSV_SEPARATOR)
.append(persone.getAddress().getCity()).append(CSV_SEPARATOR)
.append(persone.getAddress().getCountry()).append(System.lineSeparator());
});
} catch (IOException ex) {
ex.printStackTrace();
}
但对于你的情况,我会定义一个接口 CsvPrintable
public interface CsvPrintable {
String printCsv();
}
然后,类 Person 和 Address 实现了这个接口
class Person implements CsvPrintable {
String name;
int age;
Address address;
@Override
printCsv() {
return StringBuilder().append(name).append(CSV_SEPARATOR)
.append(age).append(CSV_SEPARATOR)
.append(address.printCsv()).toString();
}
}
你应该对 Address 做同样的事情。
然后我们可以改进第一个代码块:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
定义一个新的 class,例如包含 Person 和 Address 中所有字段的 PersonWithAddress。将您的人员列表转换为 PersonWithAddress 列表。现在,您可以给 openCSV 想要的东西了。
我关注 Java class 并列为:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
我想从列表中生成 CSV 文件。如果没有复合 class 胡言乱语,我可以很容易地做到这一点。你能建议我该怎么做吗?我尝试了 opencsv 并申请这样做。 Opencsv 适用于非化合物 class。我可能做错了什么。所以代码示例会有所帮助。
CSV="Comma Separated Values"。 您可以自己创建一个 csv 文件。您可以在文件中写入以逗号(或分号)分隔的值。这些值是一行的单元格。一行结束了,你写一个行分隔符。
hier 是如何以 csv 格式写入行的简单基本示例:
public static final char CSV_SEPARATOR = ';'; // it could be a comma or a semi colon
try (BufferedWriter writer = new BufferedWriter(new FileWriter("my_file.csv"))) {
personList.forEach(person -> {
writer.append(person.getName()).append(CSV_SEPARATOR)
.append(person.getAge()).append(CSV_SEPARATOR)
.append(persone.getAddress().getStreet()).append(CSV_SEPARATOR)
.append(persone.getAddress().getpostCode()).append(CSV_SEPARATOR)
.append(persone.getAddress().getCity()).append(CSV_SEPARATOR)
.append(persone.getAddress().getCountry()).append(System.lineSeparator());
});
} catch (IOException ex) {
ex.printStackTrace();
}
但对于你的情况,我会定义一个接口 CsvPrintable
public interface CsvPrintable {
String printCsv();
}
然后,类 Person 和 Address 实现了这个接口
class Person implements CsvPrintable {
String name;
int age;
Address address;
@Override
printCsv() {
return StringBuilder().append(name).append(CSV_SEPARATOR)
.append(age).append(CSV_SEPARATOR)
.append(address.printCsv()).toString();
}
}
你应该对 Address 做同样的事情。
然后我们可以改进第一个代码块:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
定义一个新的 class,例如包含 Person 和 Address 中所有字段的 PersonWithAddress。将您的人员列表转换为 PersonWithAddress 列表。现在,您可以给 openCSV 想要的东西了。