public static void csvForm(URL url) { JSONTokener tokener = null; JSONArray jsonArray = null; JSONObject place = null; String record; String[] csvRecord; String csvFileName = "data.csv"; try (CSVWriter writer = new CSVWriter(new FileWriter(csvFileName))) { tokener = new JSONTokener(url.openStream()); jsonArray = new JSONArray(tokener); // Get relevant data and write it in CSV format to file for (int i = 0; i < jsonArray.length(); i++) { place = jsonArray.getJSONObject(i); record = Integer.toString(place.getInt("_id")) + "#" + place.get("name") + "#" + place.get("type") + "#" + Double.toString(place.getJSONObject("geo_position").getDouble("latitude")) + "#" + Double.toString(place.getJSONObject("geo_position").getDouble("latitude")); System.out.println(record); csvRecord = record.split("#"); writer.writeNext(csvRecord); } } catch (IOException e) { System.err.println("Caught IOexception: " + e.getMessage()); } }
private boolean saveDataToCsvFile() { String csv = "/mnt/sdcard/DanceForHealthLog.csv"; try { CSVWriter writer = new CSVWriter(new FileWriter(csv), ','); String[] header = { "Date", "Type", "Weight", "Post-Dance HR", "Post-Dance Pedometer", "PACE Score(strain)" }; writer.writeNext(header); for (Workout w : workoutDataStore) { String[] entries = { w.getDate(), w.getType(), "" + w.getWeight(), "" + w.getHR(), "" + w.getSteps(), "" + w.getStrain() }; writer.writeNext(entries); } writer.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public static void addEntry(String db, String[] data) throws DatabaseException { checkDb(db); try (CSVWriter writer = new CSVWriter(new FileWriter(getDbFullPath(db), true))) { writer.writeNext(data); } catch (IOException e) { throw new DatabaseException("can't add entry in database!"); } }
/** * Update the ranks in the csv file when the tweet is already in the base but saved with another * mark * * @param mark : note of the tweet * @param row : line of the tweet in the base * @throws IOException */ public static void updateCSV(int mark, int row) throws IOException { CSVReader reader = new CSVReader(new FileReader(AppliSettings.filename), ';'); List<String[]> csvBody = reader.readAll(); csvBody.get(row)[4] = Integer.toString(mark); reader.close(); CSVWriter writer = new CSVWriter(new FileWriter(AppliSettings.filename), ';'); writer.writeAll(csvBody); writer.flush(); writer.close(); }
/** Data cleansing method */ public void cleanseData(AmazonS3 client) throws Exception { AwsDataLoader loader = new AwsDataLoader(); CSVReader reader = null; String prefix = loader.getS3Prefix(source); client.setEndpoint(S3_ENDPOINT); S3Object object = client.getObject(new GetObjectRequest(BUCKET_NM, prefix)); reader = new CSVReader( new BufferedReader(new InputStreamReader(object.getObjectContent())), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, HEADERS_LINE); ColumnPositionMappingStrategy<ProductLanguage> strat = new ColumnPositionMappingStrategy<ProductLanguage>(); strat.setType(ProductLanguage.class); String[] columns = new String[] {"refId", "code", "name", "locale", "state", "format", "displayNameLanguage"}; strat.setColumnMapping(columns); CsvToBean<ProductLanguage> csv = new CsvToBean<ProductLanguage>(); list = csv.parse(strat, reader); System.out.println("ProductLanguageCleanser input size: " + list.size()); this.updateDataset(list); BeanToCsv<ProductLanguage> csvWriter = new BeanToCsv<ProductLanguage>(); ByteArrayOutputStream os = new ByteArrayOutputStream(); CSVWriter writer = new CSVWriter(new OutputStreamWriter(os), ',', '"'); // strat.setColumnMapping(columns); log.info("List size: " + list.size()); csvWriter.write(strat, writer, list); writer.flush(); String dataset = os.toString(); String outPrefix = PREFIX + OUTPUT_KEY + source + ".csv"; client.setEndpoint(S3_ENDPOINT); ObjectMetadata omd = new ObjectMetadata(); try { byte[] content = dataset.getBytes(StandardCharsets.UTF_8); ByteArrayInputStream input = new ByteArrayInputStream(content); BufferedReader buffReader = new BufferedReader(new InputStreamReader(input)); buffReader.readLine(); InputStream inputObj = new ReaderInputStream(buffReader); // omd.setContentLength(IOUtils.toByteArray(input).length); client.putObject(BUCKET_NM, outPrefix, inputObj, omd); input.close(); } catch (IOException e) { log.error("Axon data write to s3 failed: " + e.getMessage()); } }
public static void exportToCSV() { ArrayList<String> keyList = new ArrayList<>(); keyList.add(DBAdapter.KEY_ROWID); keyList.add(DBAdapter.KEY_EVALUATOR); keyList.add(DBAdapter.KEY_HOSPITALID); keyList.add(DBAdapter.KEY_DATE); keyList.add(DBAdapter.KEY_LANGUAGE); for (ItemSection section : DataSource.sections) { for (ItemQuestion question : section.questions) { if (question.dbKey != null) { Collections.addAll(keyList, question.dbKey); } } } String[] keys = keyList.toArray(new String[keyList.size()]); Cursor cursor = HomeActivity.myDb.getColumns(keys); cursor.moveToFirst(); File path = Environment.getExternalStorageDirectory(); File filename = new File( path, "/FrailtyAnswers-" + cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_HOSPITALID)) + ".csv"); try { CSVWriter writer = new CSVWriter(new FileWriter(filename), '\t'); writer.writeNext(new String[] {"sep=\t"}); writer.writeNext(cursor.getColumnNames()); if (cursor.moveToFirst()) { ArrayList<String> values = new ArrayList<>(); for (int i = 0; i < cursor.getColumnCount(); i++) { values.add(cursor.getString(i)); } String[] stringValues = values.toArray(new String[values.size()]); writer.writeNext(stringValues); } writer.close(); cursor.close(); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } }
private void writeCsvRow(String[] cols) throws FileTooLargeException { csvWriter.writeNext(cols); if (countingOutputStream.getCount() > maxFileSizeBytes) { try { csvWriter.close(); } catch (IOException e) { log.error("Caught exception closing csv writer", e); } delete(); throw new FileTooLargeException(); } }
/** * Method to write a CSV List of Array of String to System Console.. * * @param content the List of Array of String to convert. * @param separator the char separator. */ public static void writeCSVDataToConsole(List<String[]> content, char separator) { try { Writer writer = new OutputStreamWriter(System.out, StringUtilities.UTF_8); CSVWriter csvWriter; if (StringUtilities.NULL_CHAR2 == separator) { csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER); } else { csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER); } csvWriter.writeAll(content, false); csvWriter.close(); } catch (IOException e) { logger.error("Can't write the CSV to Console -> " + e.getMessage(), e); } }
public static void startLogging() throws IOException { if (!loggingActive) { // String baseDir = android.os.Environment.getDataDirectory().getAbsolutePath(); String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); String fileName = DateFormat.getDateTimeInstance().format(new Date()) + ".csv"; String filePath = baseDir + File.separator + fileName; // File f = new File(filePath ); // try { csv_writer = new CSVWriter(new FileWriter(filePath.replace(":", "-"))); // } catch (IOException e) { // //e.printStackTrace(); // // } String[] data = { "Time", "TimeStamp", "Mag x", "Mag y", "Mag z", "Accel x", "Accel y", "Accel z", "Gyro x", "Gyro y", "Gyro z", "Gravity x", "Gravity y", "Gravity z" }; csv_writer.writeNext(data); loggingActive = true; } }
public void write(final String filename, final List<Payslip> payslips) { try (CSVWriter writer = new CSVWriter(new FileWriter(filename))) { final String[] headers = "Name,Start date,End date,Gross income,Income tax,Net income,Super,Error".split(","); writer.writeNext(headers); payslips.forEach( payslip -> { final String[] record = payslip.toString().split(",", -1); writer.writeNext(record); }); writer.flush(); } catch (final IOException e) { throw new IllegalStateException("Error occurred when writing to file " + filename, e); } }
/** * Method to write a CSV Data List of Beans to a String. * * @param beans the List of Beans to convert. * @param separator the char separator. * @param <T> the generic variable. * @return the String content of the List of Beans. */ public static <T> String writeCSVDataToStringWithBeans(List<T> beans, char separator) { try { Writer writer = new StringWriter(); try (CSVWriter csvWriter = new CSVWriter(writer, separator)) { List<String[]> data = toStringArray(beans); csvWriter.writeAll(data); } return writer.toString(); } catch (IOException e) { logger.error( "Can't write the CSV String from the Bean:" + beans.get(0).getClass().getName() + " -> " + e.getMessage(), e); return ""; } }
/** * Method to write a CSV Data List of Array of String to a String. * * @param content the List of Array of String to convert. * @param separator the char separator. * @return the String content of the List of Beans. */ public static String writeCSVDataToString(List<String[]> content, char separator) { try { Writer writer = new StringWriter(); CSVWriter csvWriter; if (StringUtilities.NULL_CHAR2 == separator) { csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER); } else { csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER); } csvWriter.writeAll(content); csvWriter.close(); return writer.toString(); } catch (IOException e) { logger.error("Can't write the CSV String -> " + e.getMessage(), e); return ""; } }
public static void stopLogging() throws IOException { if (loggingActive) { // try { csv_writer.close(); // } catch (IOException e) { // e.printStackTrace(); // } loggingActive = false; } }
@Override public File build() { try { csvWriter.close(); } catch (IOException e) { e.printStackTrace(); } return outputFile; }
public void produceBatterRegularSeasonStatsCSV() { Set<Player> projectedPlayers = findProjectedBatters(); List<BatterRegularSeasonStats> rsStats = projectedPlayers .stream() .map(p -> rsStatsRepo.findBySeasonAndPlayer(2015, p)) // find last season's stats .filter( s -> s != null) // some players may exist in projections but not the previous season .collect(Collectors.toList()); try { CSVWriter cw = new CSVWriter(new FileWriter("./output/batters2015.csv")); cw.writeNext(getHeaderRow()); for (BatterStats stats : rsStats) { cw.writeNext(toTuple(stats)); } cw.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
/** * Method to write a CSV File from List of Array of String. * * @param content the List of Array of String to convert. * @param separator the char separator. * @param fileOutputCsv the output File Csv to create. * @return the File Csv created. */ public static File writeCSVDataToFile( List<String[]> content, char separator, File fileOutputCsv) { try { Writer writer = new FileWriter(fileOutputCsv, true); // the true value make append the result... CSVWriter csvWriter; if (StringUtilities.NULL_CHAR2 == separator) { csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER); } else { csvWriter = new CSVWriter(writer, separator, CSVWriter.NO_QUOTE_CHARACTER); } csvWriter.writeAll(content); csvWriter.close(); return fileOutputCsv; } catch (IOException e) { logger.error("Can't write the CSV to File:" + fileOutputCsv + " -> " + e.getMessage(), e); return null; } }
public static void writeAEFile(Map<String, String> aeMap, String outfile) { try { CSVWriter writer = new CSVWriter(new FileWriter(outfile), ','); String[] header = new String[2]; header[0] = "SKU"; header[1] = "INVENTORY"; writer.writeNext(header); String[] row = new String[2]; for (String SKU : aeMap.keySet()) { row[0] = SKU; row[1] = aeMap.get(SKU); writer.writeNext(row); } writer.close(); } catch (Exception ex) { out.println(ex.getMessage() + " in writeAEFile"); } }
public static void AddLogEntry(long time_stamp) { if (loggingActive) { String[] data = { DateFormat.getTimeInstance().format(new Date()), Long.toString(time_stamp), String.valueOf(mag_reading[0]), String.valueOf(mag_reading[1]), String.valueOf(mag_reading[2]), String.valueOf(accel_reading[0]), String.valueOf(accel_reading[1]), String.valueOf(accel_reading[2]), String.valueOf(gyro_reading[0]), String.valueOf(gyro_reading[1]), String.valueOf(gyro_reading[2]), String.valueOf(gravity_accel[0]), String.valueOf(gravity_accel[1]), String.valueOf(gravity_accel[2]) }; csv_writer.writeNext(data); } }
@RequestMapping("/events/{eventName}/export.csv") public void downloadAllTicketsCSV( @PathVariable("eventName") String eventName, HttpServletRequest request, HttpServletResponse response, Principal principal) throws IOException { List<String> fields = Arrays.asList( Optional.ofNullable(request.getParameterValues("fields")).orElse(new String[] {})); Event event = loadEvent(eventName, principal); Map<Integer, TicketCategory> categoriesMap = eventManager .loadTicketCategories(event) .stream() .collect(Collectors.toMap(TicketCategory::getId, Function.identity())); ZoneId eventZoneId = event.getZoneId(); Predicate<String> contains = FIXED_FIELDS::contains; response.setContentType("text/csv;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + eventName + "-export.csv"); try (ServletOutputStream out = response.getOutputStream(); CSVWriter writer = new CSVWriter(new OutputStreamWriter(out))) { for (int marker : BOM_MARKERS) { // UGLY-MODE_ON: specify that the file is written in UTF-8 with BOM, thanks // to alexr http://stackoverflow.com/a/4192897 out.write(marker); } writer.writeNext(fields.toArray(new String[fields.size()])); eventManager .findAllConfirmedTickets(eventName, principal.getName()) .stream() .map( t -> { List<String> line = new ArrayList<>(); if (fields.contains("ID")) { line.add(t.getUuid()); } if (fields.contains("creation")) { line.add(t.getCreation().withZoneSameInstant(eventZoneId).toString()); } if (fields.contains("category")) { line.add(categoriesMap.get(t.getCategoryId()).getName()); } if (fields.contains("event")) { line.add(eventName); } if (fields.contains("status")) { line.add(t.getStatus().toString()); } if (fields.contains("originalPrice")) { line.add(MonetaryUtil.centsToUnit(t.getSrcPriceCts()).toString()); } if (fields.contains("paidPrice")) { line.add(MonetaryUtil.centsToUnit(t.getFinalPriceCts()).toString()); } if (fields.contains("discount")) { line.add(MonetaryUtil.centsToUnit(t.getDiscountCts()).toString()); } if (fields.contains("vat")) { line.add(MonetaryUtil.centsToUnit(t.getVatCts()).toString()); } if (fields.contains("reservationID")) { line.add(t.getTicketsReservationId()); } if (fields.contains("Full Name")) { line.add(t.getFullName()); } if (fields.contains("First Name")) { line.add(t.getFirstName()); } if (fields.contains("Last Name")) { line.add(t.getLastName()); } if (fields.contains("E-Mail")) { line.add(t.getEmail()); } if (fields.contains("locked")) { line.add(String.valueOf(t.getLockedAssignment())); } if (fields.contains("Language")) { line.add(String.valueOf(t.getUserLanguage())); } // obviously not optimized Map<String, String> additionalValues = ticketFieldRepository.findAllValuesForTicketId(t.getId()); fields .stream() .filter(contains.negate()) .forEachOrdered( field -> { line.add(additionalValues.getOrDefault(field, "").replaceAll("\"", "")); }); return line.toArray(new String[line.size()]); }) .forEachOrdered(writer::writeNext); writer.flush(); out.flush(); } }
@RequestMapping("/events/{eventName}/sponsor-scan/export.csv") public void downloadSponsorScanExport( @PathVariable("eventName") String eventName, HttpServletResponse response, Principal principal) throws IOException { Event event = loadEvent(eventName, principal); List<TicketFieldConfiguration> fields = ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()); response.setContentType("text/csv;charset=UTF-8"); response.setHeader( "Content-Disposition", "attachment; filename=" + eventName + "-sponsor-scan.csv"); try (ServletOutputStream out = response.getOutputStream(); CSVWriter writer = new CSVWriter(new OutputStreamWriter(out))) { for (int marker : BOM_MARKERS) { out.write(marker); } List<String> header = new ArrayList<>(); header.add("Username"); header.add("Timestamp"); header.add("Full name"); header.add("Email"); header.addAll( fields.stream().map(TicketFieldConfiguration::getName).collect(Collectors.toList())); writer.writeNext(header.toArray(new String[header.size()])); userManager .findAllUsers(principal.getName()) .stream() .map(u -> Pair.of(u, userManager.getUserRole(u))) .filter(p -> p.getRight() == Role.SPONSOR) .flatMap( p -> sponsorScanRepository .loadSponsorData( event.getId(), p.getKey().getId(), SponsorScanRepository.DEFAULT_TIMESTAMP) .stream() .map( v -> Pair.of( v, ticketFieldRepository.findAllValuesForTicketId( v.getTicket().getId())))) .map( p -> { DetailedScanData data = p.getLeft(); Map<String, String> descriptions = p.getRight(); return Pair.of( data, fields .stream() .map(x -> descriptions.getOrDefault(x.getName(), "")) .collect(Collectors.toList())); }) .map( p -> { List<String> line = new ArrayList<>(); Ticket ticket = p.getLeft().getTicket(); SponsorScan sponsorScan = p.getLeft().getSponsorScan(); line.add(userManager.findUser(sponsorScan.getUserId()).getUsername()); line.add(sponsorScan.getTimestamp().toString()); line.add(ticket.getFullName()); line.add(ticket.getEmail()); line.addAll(p.getRight()); return line.toArray(new String[line.size()]); }) .forEachOrdered(writer::writeNext); writer.flush(); out.flush(); } }
public void writeText(String filepath) { CSVWriter writer; try { writer = new CSVWriter(new FileWriter(filepath), ',', CSVWriter.NO_QUOTE_CHARACTER); // feed in your array (or convert your data to an array) writer.writeAll(ioNames.unformattedStrings()); writer.close(); } catch (IOException ex) { System.out.println("Write text error with " + filepath + " : " + ex.getMessage()); } }