Java Code Examples for org.apache.commons.csv.CSVRecord#forEach()

The following examples show how to use org.apache.commons.csv.CSVRecord#forEach() . 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: DownloadUtil.java    From yanagishima with Apache License 2.0 8 votes vote down vote up
private static void download(HttpServletResponse response, Path resultFilePath, String encode, boolean showHeader, boolean showBOM, char delimiter) {
    try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), encode))) {
        try (BufferedReader reader = Files.newBufferedReader(resultFilePath);
            CSVPrinter printer = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(delimiter).withRecordSeparator(System.getProperty("line.separator")))) {
            CSVParser parser = CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").parse(reader);

            if (showBOM) {
                response.getOutputStream().write(BOM);
            }
            int rowNumber = 0;
            for (CSVRecord record : parser) {
                List<String> columns = new ArrayList<>();
                record.forEach(columns::add);

                if (showHeader || rowNumber > 0) {
                    printer.printRecord(columns);
                }
                rowNumber++;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: DLPTextToBigQueryStreaming.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
private static List<String> getFileHeaders(BufferedReader reader) {
  List<String> headers = new ArrayList<>();
  try {
    CSVRecord csvHeader = CSVFormat.DEFAULT.parse(reader).getRecords().get(0);
    csvHeader.forEach(
        headerValue -> {
          headers.add(headerValue);
        });
  } catch (IOException e) {
    LOG.error("Failed to get csv header values}", e.getMessage());
    throw new RuntimeException(e);
  }
  return headers;
}
 
Example 3
Source File: DLPTextToBigQueryStreaming.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private static List<String> getFileHeaders(BufferedReader reader) {
  List<String> headers = new ArrayList<>();
  try {
    CSVRecord csvHeader = CSVFormat.DEFAULT.parse(reader).getRecords().get(0);
    csvHeader.forEach(
        headerValue -> {
          headers.add(headerValue);
        });
  } catch (IOException e) {
    LOG.error("Failed to get csv header values}", e.getMessage());
    throw new RuntimeException(e);
  }
  return headers;
}