How to generate CSV file in Java from the List where List has compound class
I have following Java class and list as:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
I would like to generate s CSV file from the list. I could easily do it if there were not compound class hariacy. Can you please suggest how can I do it? I tried opencsv and filed to do it. Opencsv works for noncompound class. I might have done something wrong. So code example will be helpful.
java csv export-to-csv opencsv
add a comment |
I have following Java class and list as:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
I would like to generate s CSV file from the list. I could easily do it if there were not compound class hariacy. Can you please suggest how can I do it? I tried opencsv and filed to do it. Opencsv works for noncompound class. I might have done something wrong. So code example will be helpful.
java csv export-to-csv opencsv
add a comment |
I have following Java class and list as:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
I would like to generate s CSV file from the list. I could easily do it if there were not compound class hariacy. Can you please suggest how can I do it? I tried opencsv and filed to do it. Opencsv works for noncompound class. I might have done something wrong. So code example will be helpful.
java csv export-to-csv opencsv
I have following Java class and list as:-
class Person{
String name;
int age;
Address address;
}
class Address{
String street;
String post_code;
String city;
String country;
}
List<Person> personList = new ArrayList<>();
I would like to generate s CSV file from the list. I could easily do it if there were not compound class hariacy. Can you please suggest how can I do it? I tried opencsv and filed to do it. Opencsv works for noncompound class. I might have done something wrong. So code example will be helpful.
java csv export-to-csv opencsv
java csv export-to-csv opencsv
asked Nov 20 at 11:01
masiboo
1,25932455
1,25932455
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
CSV= "Comma Separated Values".
You can create by yourself a csv file. You could write in the file values separated with a comma (or semi-colon). those values are cells of one row. a row is over wenn you write a line separator.
hier is a simple and basic sample of how to write rows in a csv format:
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();
}
but for your case, I would define an interface CsvPrintable
public interface CsvPrintable {
String printCsv();
}
then, classes Person and Address implements this interface
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();
}
}
You should do the same thing with Address.
then we can improve the first code block:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
add a comment |
Define a new class, for example PersonWithAddress that includes all fields from Person and Address. Transform your Person List into a PersonWithAddress List. Now, you can give openCSV what it wants.
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391561%2fhow-to-generate-csv-file-in-java-from-the-list-where-list-has-compound-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
CSV= "Comma Separated Values".
You can create by yourself a csv file. You could write in the file values separated with a comma (or semi-colon). those values are cells of one row. a row is over wenn you write a line separator.
hier is a simple and basic sample of how to write rows in a csv format:
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();
}
but for your case, I would define an interface CsvPrintable
public interface CsvPrintable {
String printCsv();
}
then, classes Person and Address implements this interface
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();
}
}
You should do the same thing with Address.
then we can improve the first code block:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
add a comment |
CSV= "Comma Separated Values".
You can create by yourself a csv file. You could write in the file values separated with a comma (or semi-colon). those values are cells of one row. a row is over wenn you write a line separator.
hier is a simple and basic sample of how to write rows in a csv format:
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();
}
but for your case, I would define an interface CsvPrintable
public interface CsvPrintable {
String printCsv();
}
then, classes Person and Address implements this interface
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();
}
}
You should do the same thing with Address.
then we can improve the first code block:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
add a comment |
CSV= "Comma Separated Values".
You can create by yourself a csv file. You could write in the file values separated with a comma (or semi-colon). those values are cells of one row. a row is over wenn you write a line separator.
hier is a simple and basic sample of how to write rows in a csv format:
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();
}
but for your case, I would define an interface CsvPrintable
public interface CsvPrintable {
String printCsv();
}
then, classes Person and Address implements this interface
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();
}
}
You should do the same thing with Address.
then we can improve the first code block:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
CSV= "Comma Separated Values".
You can create by yourself a csv file. You could write in the file values separated with a comma (or semi-colon). those values are cells of one row. a row is over wenn you write a line separator.
hier is a simple and basic sample of how to write rows in a csv format:
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();
}
but for your case, I would define an interface CsvPrintable
public interface CsvPrintable {
String printCsv();
}
then, classes Person and Address implements this interface
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();
}
}
You should do the same thing with Address.
then we can improve the first code block:
personList.forEach(person -> {
writer.append(person.printCsv()).append((System.lineSeparator());
});
edited Nov 20 at 12:07
answered Nov 20 at 11:18
Heny Kamoun
364
364
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
add a comment |
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:04
add a comment |
Define a new class, for example PersonWithAddress that includes all fields from Person and Address. Transform your Person List into a PersonWithAddress List. Now, you can give openCSV what it wants.
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
add a comment |
Define a new class, for example PersonWithAddress that includes all fields from Person and Address. Transform your Person List into a PersonWithAddress List. Now, you can give openCSV what it wants.
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
add a comment |
Define a new class, for example PersonWithAddress that includes all fields from Person and Address. Transform your Person List into a PersonWithAddress List. Now, you can give openCSV what it wants.
Define a new class, for example PersonWithAddress that includes all fields from Person and Address. Transform your Person List into a PersonWithAddress List. Now, you can give openCSV what it wants.
answered Nov 20 at 11:49
tikeman
111
111
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
add a comment |
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
Actually, I posted some simple code get some idea. The actual object is much more complex than the given code. I need both json and csv file. I can easily generate json by Jackson api. I found Jackson has also csv here github.com/FasterXML/jackson-dataformats-text/tree/master/csv. But it ways it can work for POJOs. I don't know will this work for a complex objeect?
– masiboo
Nov 20 at 14:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391561%2fhow-to-generate-csv-file-in-java-from-the-list-where-list-has-compound-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown