org.supercsv.cellprocessor.ConvertNullTo Java Examples
The following examples show how to use
org.supercsv.cellprocessor.ConvertNullTo.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Serialization.java From joinery with GNU General Public License v3.0 | 6 votes |
public static <V> void writeCsv(final DataFrame<V> df, final OutputStream output) throws IOException { try (CsvListWriter writer = new CsvListWriter(new OutputStreamWriter(output), CsvPreference.STANDARD_PREFERENCE)) { final String[] header = new String[df.size()]; final Iterator<Object> it = df.columns().iterator(); for (int c = 0; c < df.size(); c++) { header[c] = String.valueOf(it.hasNext() ? it.next() : c); } writer.writeHeader(header); final CellProcessor[] procs = new CellProcessor[df.size()]; final List<Class<?>> types = df.types(); for (int c = 0; c < df.size(); c++) { final Class<?> cls = types.get(c); if (Date.class.isAssignableFrom(cls)) { procs[c] = new ConvertNullTo("", new FmtDate("yyyy-MM-dd'T'HH:mm:ssXXX")); } else { procs[c] = new ConvertNullTo(""); } } for (final List<V> row : df) { writer.write(row, procs); } } }
Example #2
Source File: Writing.java From super-csv with Apache License 2.0 | 4 votes |
/** * An example of partial reading using CsvDozerBeanWriter. */ private static void partialWriteWithCsvDozerBeanWriter() throws Exception { // ignore questionNo/answer 1 columns final String[] partialFieldMapping = new String[] { "age", "consentGiven", "answers[1].questionNo", "answers[1].answer", "answers[2].questionNo", "answers[2].answer" }; // ignore questionNo/answer 1 columns keep up with partialFieldMapping final CellProcessor[] partialProcessors = new CellProcessor[] { new Token(0, "age not supplied"), // age new FmtBool("Y", "N"), // consent new NotNull(), // questionNo 2 new ConvertNullTo("not answered"), // answer 2 new NotNull(), // questionNo 3 new ConvertNullTo("not answered")}; // answer 3 // create the survey responses to write SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2, "Albert Einstein"), new Answer(3, "Big Bang Theory"))); SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2, "Nikola Tesla"), new Answer(3, "Stargate"))); SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2, "Carl Sagan"), new Answer(3, "Star Wars"))); final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3); ICsvDozerBeanWriter beanWriter = null; try { beanWriter = new CsvDozerBeanWriter(new FileWriter("target/partialWriteWithCsvDozerBeanWriter.csv"), CsvPreference.STANDARD_PREFERENCE); // configure the mapping from the fields to the CSV columns beanWriter.configureBeanMapping(SurveyResponse.class, partialFieldMapping); // write the header beanWriter.writeHeader("age", "consentGiven", "questionNo2", "answer2", "questionNo3", "answer3"); // write the beans for( final SurveyResponse surveyResponse : surveyResponses ) { beanWriter.write(surveyResponse, partialProcessors); } } finally { if( beanWriter != null ) { beanWriter.close(); } } }