org.supercsv.io.CsvBeanWriter Java Examples
The following examples show how to use
org.supercsv.io.CsvBeanWriter.
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: MainController.java From video-streaming-service with MIT License | 6 votes |
@RequestMapping("download-csv/{id}") public void downloadCSV(HttpServletResponse response, @PathVariable("id") Long id) throws IOException { final Material material = materialRepository.findOne(id); if (material == null) throw new IllegalArgumentException("[" + id + "] data is not exist."); String videoName = material.getVideoName(); int lastIndexOf = videoName.lastIndexOf("/"); videoName = lastIndexOf >= 0 ? videoName.substring(lastIndexOf + 1, videoName.length()) : videoName; final String csvFileName = videoName + "_" + (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(material.getCreatedAt())) + ".csv"; response.setContentType("text/csv"); String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", csvFileName); response.setHeader(headerKey, headerValue); final ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE); final String[] header = {"timestamp", "key"}; csvWriter.writeHeader(header); for (MaterialData data : material.getMaterialDataList()) csvWriter.write(data, header); csvWriter.close(); }
Example #2
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 6 votes |
@Test public void testDateSupport() throws IOException { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 1999); calendar.set(Calendar.MONTH, 6); calendar.set(Calendar.DAY_OF_MONTH, 12); FeatureBean character = new FeatureBean("John", "Connor", 16); character.setBirthDate(calendar.getTime()); String[] mapping = { "birthDate" }; DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance()); CellProcessor[] processors = { new FmtDate("yyyy-MM-dd") }; StringWriter writer = new StringWriter(); CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE); beanWriter.write(character, mapping, processors); beanWriter.close(); String csv = writer.toString(); Assert.assertNotNull(csv); Assert.assertEquals("1999-07-12\r\n", csv); }
Example #3
Source File: CitationsFileWriter.java From occurrence with Apache License 2.0 | 6 votes |
/** * Creates the dataset citation file using the the search query response. * * @param datasetUsages record count per dataset * @param citationFileName output file name * @param occDownloadService occurrence downlaod service * @param downloadKey download key */ public static void createCitationFile(Map<UUID, Long> datasetUsages, String citationFileName, OccurrenceDownloadService occDownloadService, String downloadKey) { if (datasetUsages != null && !datasetUsages.isEmpty()) { try (ICsvBeanWriter beanWriter = new CsvBeanWriter(new FileWriterWithEncoding(citationFileName, Charsets.UTF_8), CsvPreference.TAB_PREFERENCE)) { for (Entry<UUID, Long> entry : datasetUsages.entrySet()) { if (entry.getKey() != null) { beanWriter.write(new Facet.Count(entry.getKey().toString(), entry.getValue()), HEADER, PROCESSORS); } } beanWriter.flush(); persistUsages(occDownloadService, downloadKey, datasetUsages); } catch (IOException e) { LOG.error("Error creating citations file", e); throw Throwables.propagate(e); } } }
Example #4
Source File: CsvTickWriter.java From AlgoTrader with GNU General Public License v2.0 | 5 votes |
public CsvTickWriter(String symbol) throws IOException { File file = new File("results/tickdata/" + dataSet + "/" + symbol + ".csv"); boolean exists = file.exists(); this.writer = new CsvBeanWriter(new FileWriter(file, true), CsvPreference.EXCEL_PREFERENCE); if (!exists) { this.writer.writeHeader(header); } }
Example #5
Source File: CSVExporter.java From robe with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void exportStream(OutputStream outputStream, Iterator<T> iterator) throws IOException, ClassNotFoundException, IllegalAccessException { if (iterator == null) throw new NullPointerException("List can not be null or empty."); Writer writer = new OutputStreamWriter(outputStream, "UTF-8"); ICsvBeanWriter beanWriter = new CsvBeanWriter(writer, preference); while (iterator.hasNext()) { T entry = iterator.next(); beanWriter.write(entry, fieldNames, processors); } beanWriter.flush(); }
Example #6
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 5 votes |
@Test public void testColumnNameBasedMapping() throws IOException { FeatureBean character = new FeatureBean("John", "Connor", 16); String[] mapping = { "lastName", "firstName" }; StringWriter writer = new StringWriter(); CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE); beanWriter.write(character, mapping); beanWriter.close(); String csv = writer.toString(); Assert.assertNotNull(csv); Assert.assertEquals("Connor,John\r\n", csv); }
Example #7
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 5 votes |
@Test public void testConvertsToPrimitives() throws IOException { FeatureBean character = new FeatureBean("John", "Connor", 16); String[] mapping = { "lastName", "firstName", "age" }; StringWriter writer = new StringWriter(); CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE); beanWriter.write(character, mapping); beanWriter.close(); String csv = writer.toString(); Assert.assertNotNull(csv); Assert.assertEquals("Connor,John,16\r\n", csv); }
Example #8
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 5 votes |
@Test public void testConvertsToBasicObjects() throws IOException { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 1999); calendar.set(Calendar.MONTH, 6); calendar.set(Calendar.DAY_OF_MONTH, 12); FeatureBean character = new FeatureBean("John", "Connor", 16); character.setSavings(new BigDecimal(6.65)); character.setBirthDate(calendar.getTime()); String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" }; DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance()); CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"), new FmtNumber(formatter) }; StringWriter writer = new StringWriter(); CsvPreference customPreference = new Builder('"', '|', "\r\n").build(); CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference); beanWriter.write(character, mapping, processors); beanWriter.close(); String csv = writer.toString(); Assert.assertNotNull(csv); Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv); }
Example #9
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 5 votes |
@Test public void testConverterSupport() throws IOException { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 1999); calendar.set(Calendar.MONTH, 6); calendar.set(Calendar.DAY_OF_MONTH, 12); FeatureBean character = new FeatureBean("John", "Connor", 16); character.setSavings(new BigDecimal(6.65)); character.setBirthDate(calendar.getTime()); String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" }; DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance()); CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"), new FmtNumber(formatter) }; StringWriter writer = new StringWriter(); CsvPreference customPreference = new Builder('"', '|', "\r\n").build(); CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference); beanWriter.write(character, mapping, processors); beanWriter.close(); String csv = writer.toString(); Assert.assertNotNull(csv); Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv); }
Example #10
Source File: WritingFeaturesTest.java From super-csv with Apache License 2.0 | 5 votes |
@Test public void testWriteToWriter() throws IOException { StringWriter writer = new StringWriter(); new CsvListWriter(writer, STANDARD_PREFERENCE); new CsvMapWriter(writer, STANDARD_PREFERENCE); new CsvBeanWriter(writer, STANDARD_PREFERENCE); }
Example #11
Source File: ReadWriteCSV.java From AIDR with GNU Affero General Public License v3.0 | 5 votes |
public ICsvBeanWriter getCSVBeanWriter(String fileToWrite) { try { return new CsvBeanWriter(new FileWriter(fileToWrite, true), new CsvPreference.Builder(CsvPreference.EXCEL_PREFERENCE) .useEncoder(new DefaultCsvEncoder()) .build() ); } catch (IOException e) { // TODO Auto-generated catch block logger.error("Error in creating CSV Bean writer!"); logger.error("Exception",e); } return null; }
Example #12
Source File: DownloadDwcaActor.java From occurrence with Apache License 2.0 | 5 votes |
/** * Executes the job.query and creates a data file that will contains the records from job.from to job.to positions. */ public void doWork(DownloadFileWork work) throws IOException { DatasetUsagesCollector datasetUsagesCollector = new DatasetUsagesCollector(); try ( ICsvMapWriter intCsvWriter = new CsvMapWriter(new FileWriterWithEncoding(work.getJobDataFileName() + TableSuffixes.INTERPRETED_SUFFIX, Charsets.UTF_8), CsvPreference.TAB_PREFERENCE); ICsvMapWriter verbCsvWriter = new CsvMapWriter(new FileWriterWithEncoding(work.getJobDataFileName() + TableSuffixes.VERBATIM_SUFFIX, Charsets.UTF_8), CsvPreference.TAB_PREFERENCE); ICsvBeanWriter multimediaCsvWriter = new CsvBeanWriter(new FileWriterWithEncoding(work.getJobDataFileName() + TableSuffixes.MULTIMEDIA_SUFFIX, Charsets.UTF_8), CsvPreference.TAB_PREFERENCE)) { SearchQueryProcessor.processQuery(work, occurrence -> { try { // Writes the occurrence record obtained from Elasticsearch as Map<String,Object>. if (occurrence != null) { datasetUsagesCollector.incrementDatasetUsage(occurrence.getDatasetKey().toString()); intCsvWriter.write(OccurrenceMapReader.buildInterpretedOccurrenceMap(occurrence), INT_COLUMNS); verbCsvWriter.write(OccurrenceMapReader.buildVerbatimOccurrenceMap(occurrence), VERB_COLUMNS); writeMediaObjects(multimediaCsvWriter, occurrence); } } catch (Exception e) { throw Throwables.propagate(e); } }); } finally { // Unlock the assigned lock. work.getLock().unlock(); LOG.info("Lock released, job detail: {} ", work); } getSender().tell(new Result(work, datasetUsagesCollector.getDatasetUsages()), getSelf()); }