-
-
Save shsdev/11392809 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | |
import com.fasterxml.jackson.databind.ObjectWriter; | |
import com.fasterxml.jackson.dataformat.csv.CsvMapper; | |
import com.fasterxml.jackson.dataformat.csv.CsvSchema; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Using jackson-dataformat-csv to create CSV file out of POJOs (bean class). | |
* Use with maven-dependency: | |
* <dependency> | |
* <groupId>com.fasterxml.jackson.dataformat</groupId> | |
* <artifactId>jackson-dataformat-csv</artifactId> | |
* <version>2.3.3</version> | |
* </dependency> | |
* https://github.com/FasterXML/jackson-dataformat-csv | |
*/ | |
public class CsvCreator { | |
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException { | |
// POJO (bean class) | |
@JsonPropertyOrder({"name", "age"}) | |
class User { | |
public String name; | |
public int age; | |
public User() { | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
} | |
// define objects | |
User user1 = new User(); | |
user1.name = "foo"; | |
user1.age = 32; | |
User user2 = new User(); | |
user2.name = "bar"; | |
user2.age = 27; | |
List<User> users = new ArrayList<User>(); | |
users.add(user1); | |
users.add(user2); | |
// create mapper and schema | |
CsvMapper mapper = new CsvMapper(); | |
CsvSchema schema = mapper.schemaFor(User.class); | |
schema = schema.withColumnSeparator('\t'); | |
// output writer | |
ObjectWriter myObjectWriter = mapper.writer(schema); | |
File tempFile = new File("users.csv"); | |
FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile); | |
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(tempFileOutputStream, 1024); | |
OutputStreamWriter writerOutputStream = new OutputStreamWriter(bufferedOutputStream, "UTF-8"); | |
myObjectWriter.writeValue(writerOutputStream, users); | |
} | |
} |
Why are you using object writer and outputstreams instead of csvmapper.writevalue()?
Correct, it's easier to create the CSV as you propose.
Got NullPointerException at line 72.
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:394) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:365) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:338) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:123) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1120) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:966) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
Thanks
Thanks
Why are you using object writer and outputstreams instead of csvmapper.writevalue()?