示例#1
0
  /**
   * Translates the given CSV content into a List of objects.
   *
   * <p>Translation of each row into its representative object is handled by the {@link
   * RowTranslator} provided during construction. This class only handles the generic iteration
   * logic and global error handling.
   *
   * <p><strong>Warning:</strong> the given CSV content is read completely into memory while
   * parsing. This method does not therefore support very large CSV files (i.e., 50MB+).
   *
   * @param csvReader a Reader providing the CSV content. This method will close the reader when
   *     done.
   */
  public TabularParseResult<T> readObjects(final Reader csvReader) {
    final ArrayList<TabularUploadError> errors = new ArrayList<>();

    try {
      final List<CSVRecord> records = CSVFormat.EXCEL.parse(csvReader).getRecords();
      LOGGER.debug("The CSV file has {} rows.", records.size());

      if (records.isEmpty()) {
        errors.add(TabularUploadError.global(ValidationCodes.NO_VALUE, null, "no records"));
      } else {
        return readProcedures(records, errors);
      }
    } catch (final IOException ex) {
      errors.add(
          TabularUploadError.global(
              ValidationCodes.INVALID_CONTENTS, null, "Could not parse the CSV content."));
    } finally {
      try {
        csvReader.close();
      } catch (final IOException ex) {
        LOGGER.warn("Failed to close the CSV reader.", ex);
      }
    }

    return new TabularParseResult<>(Collections.<T>emptyList(), errors);
  }
 /**
  * Executa carregamento da matriz de adjacência por um arquivo CSV
  *
  * @param file Arquivo CSV
  * @throws IOException Problema no momento da leitura do arquivo CSV
  */
 public void loadCsv(File file) throws IOException {
   Reader in = new FileReader(file);
   Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
   for (CSVRecord record : records) {
     List<Integer> linha = new ArrayList<>();
     Iterator<String> linhaCsv = record.iterator();
     while (linhaCsv.hasNext()) {
       try {
         linha.add(Integer.parseInt(linhaCsv.next()));
       } catch (NumberFormatException e) {
         throw new RuntimeException("Não foi possivel converter arquivo!", e);
       }
     }
     mapa.add(linha);
   }
 }