Java Code Examples for com.opencsv.CSVReader#readAll()

The following examples show how to use com.opencsv.CSVReader#readAll() . 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: CsvDataProvider.java    From NoraUi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String[] readLine(int line, boolean readResult) {
    log.debug("readLine at line {}", line);
    try {
        final CSVReader reader = openInputData();
        final List<String[]> a = reader.readAll();
        if (line >= a.size()) {
            return null;
        }
        final String[] row = a.get(line);
        if ("".equals(row[0])) {
            return null;
        } else {
            final String[] ret = readResult ? new String[columns.size()] : new String[columns.size() - 1];
            System.arraycopy(row, 0, ret, 0, ret.length);
            return ret;
        }
    } catch (final IOException e) {
        log.error("error CsvDataProvider.readLine()", e);
        return null;
    }
}
 
Example 2
Source File: CsvDataProvider.java    From NoraUi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void writeValue(String column, int line, String value) {
    log.debug("Writing: [{}] at line [{}] in column [{}]", value, line, column);
    final int colIndex = columns.indexOf(column);
    CSVReader reader;
    try {
        reader = openOutputData();
        final List<String[]> csvBody = reader.readAll();
        csvBody.get(line)[colIndex] = value;
        reader.close();
        writeValue(column, line, value, csvBody);
    } catch (final IOException e1) {
        log.error(Messages.getMessage(CSV_DATA_PROVIDER_WRITING_IN_CSV_ERROR_MESSAGE), column, line, value, e1);
    }
}
 
Example 3
Source File: CsvReaderExamples.java    From tutorials with MIT License 6 votes vote down vote up
public static List<String[]> readAll(Reader reader) {

        CSVParser parser = new CSVParserBuilder()
                .withSeparator(',')
                .withIgnoreQuotations(true)
                .build();

        CSVReader csvReader = new CSVReaderBuilder(reader)
                .withSkipLines(0)
                .withCSVParser(parser)
                .build();

        List<String[]> list = new ArrayList<>();
        try {
            list = csvReader.readAll();
            reader.close();
            csvReader.close();
        } catch (Exception ex) {
            Helpers.err(ex);
        }
        return list;
    }
 
Example 4
Source File: CsvReaderService.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
private DataFileDto readFile(final CSVReader csvReader, final Boolean readFirstColumnAsColumnName) throws IOException {
    final List<String[]> allDataInFile = csvReader.readAll();
    final Integer typeColumnIndex = readFirstColumnAsColumnName ? HEADER_COLUMN_INDEX + 1 : 0;

    final DataFileDto dataFileDto = new DataFileDto();
    for (int i = 0; i < allDataInFile.size(); i++) {
        if (i == typeColumnIndex) {
            dataFileDto.setRowTypes(getLineAsRowInMemory(allDataInFile.get(i)));
        } else {
            dataFileDto.addRow(getLineAsRowInMemory(allDataInFile.get(i)));
        }
    }
    return dataFileDto;
}
 
Example 5
Source File: ApplicationConfiguration.java    From StatsAgg with Apache License 2.0 5 votes vote down vote up
private static List<InfluxdbV1HttpOutputModule> readInfluxdbV1HttpOutputModules() {
    
    List<InfluxdbV1HttpOutputModule> influxdbV1HttpOutputModules = new ArrayList<>();
    
    for (int i = -1; i < 10000; i++) {
        String influxdbV1HttpOutputModuleKey = "influxdb_v1_output_module_" + (i + 1);
        String influxdbV1HttpOutputModuleValue = applicationConfiguration_.safeGetString(influxdbV1HttpOutputModuleKey, null);
        
        if (influxdbV1HttpOutputModuleValue == null) continue;
        
        try {
            CSVReader reader = new CSVReader(new StringReader(influxdbV1HttpOutputModuleValue));
            List<String[]> csvValuesArray = reader.readAll();

            if ((csvValuesArray != null) && !csvValuesArray.isEmpty() && (csvValuesArray.get(0) != null)) {
                String[] csvValues = csvValuesArray.get(0);

                if (csvValues.length == 4) {                                
                    boolean isOutputEnabled = Boolean.valueOf(csvValues[0]);
                    String url = csvValues[1];
                    int numSendRetryAttempts = Integer.valueOf(csvValues[2]);
                    int maxMetricsPerMessage = Integer.valueOf(csvValues[3]);
                    
                    String uniqueId = "InfluxDB-V1-" + (i+1);

                    InfluxdbV1HttpOutputModule influxdbV1HttpOutputModule = new InfluxdbV1HttpOutputModule(isOutputEnabled, url, 
                            numSendRetryAttempts, maxMetricsPerMessage, uniqueId);
                    
                    influxdbV1HttpOutputModules.add(influxdbV1HttpOutputModule);
                }
            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
        }
    }
    
    return influxdbV1HttpOutputModules;
}
 
Example 6
Source File: ApplicationConfiguration.java    From StatsAgg with Apache License 2.0 5 votes vote down vote up
private static List<HttpLink> readCustomActionUrls() {
    
    List<HttpLink> customActionUrls = new ArrayList<>();
    
    for (int i = 0; i < 1000; i++) {
        String customActionUrlKey = "custom_action_url_" + (i + 1);
        String customActionUrlValue = applicationConfiguration_.safeGetString(customActionUrlKey, null);
        
        if (customActionUrlValue == null) continue;
        
        try {
            CSVReader reader = new CSVReader(new StringReader(customActionUrlValue));
            List<String[]> csvValuesArray = reader.readAll();

            if ((csvValuesArray != null) && !csvValuesArray.isEmpty() && (csvValuesArray.get(0) != null)) {
                String[] csvValues = csvValuesArray.get(0);

                if (csvValues.length == 2) {                                
                    String url = csvValues[0];
                    String linkText = csvValues[1];
                    
                    HttpLink httpLink = new HttpLink(url, linkText);
                    customActionUrls.add(httpLink);
                }

            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
        }
        
    }
    
    return customActionUrls;
}
 
Example 7
Source File: CSVReaderService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean initialize(MetaFile input, String separator) {

  if (input == null) {
    return false;
  }

  fileName = input.getFileName().replaceAll(".csv", "");
  File inFile = MetaFiles.getPath(input).toFile();
  if (!inFile.exists()) {
    return false;
  }

  try (FileInputStream inSteam = new FileInputStream(inFile)) {

    csvReader =
        new CSVReader(
            new InputStreamReader(inSteam, StandardCharsets.UTF_8), separator.charAt(0));
    totalRows = csvReader.readAll();
    if (csvReader.getLinesRead() == 0) {
      return false;
    }
  } catch (IOException e) {
    LOG.error(e.getMessage());
    return false;
  }
  return true;
}
 
Example 8
Source File: CsvDecoder.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
private String[] parseCsv(final String string) {
    String[] parts = new String[0];
    try {
        final CSVReader reader = new CSVReader(new StringReader(string),
                separator);
        final List<String[]> lines = reader.readAll();
        if (lines.size() > 0) {
            parts = lines.get(0);
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return parts;
}
 
Example 9
Source File: InstExpCsv.java    From CQL with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Expects filenames in the map
 */
public static Map<En, List<String[]>> start2(Map<String, Reader> map, AqlOptions op,
		Schema<Ty, En, Sym, Fk, Att> sch, boolean omitCheck) throws Exception {
	Character sepChar = (Character) op.getOrDefault(AqlOption.csv_field_delim_char);
	Character quoteChar = (Character) op.getOrDefault(AqlOption.csv_quote_char);
	Character escapeChar = (Character) op.getOrDefault(AqlOption.csv_escape_char);

	final CSVParser parser = new CSVParserBuilder().withSeparator(sepChar).withQuoteChar(quoteChar)
			.withEscapeChar(escapeChar).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();

	Map<En, List<String[]>> ret = new THashMap<>();
	for (String k : map.keySet()) {
		if (!omitCheck) {
			if (!sch.ens.contains(En.En(k))) {
				throw new RuntimeException("Not an entity: " + k);
			}
		}
		Reader r = map.get(k);

		// File file = new File(map.get(k));
		// BufferedReader fileReader = new BufferedReader(r);
		// String s;
		/// while ((s = fileReader.readLine()) != null) {
		// System.out.println(s);
		// }

		final CSVReader reader = new CSVReaderBuilder(r).withCSVParser(parser)
				.withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();

		List<String[]> rows = reader.readAll();
		
		ret.put(En.En(k), rows);
		reader.close();
		r.close();
	}

	if (!omitCheck) {
		for (En en : sch.ens) {
			if (!ret.containsKey(en)) {
				ret.put(en,
						new LinkedList<>(Collections.singletonList(Util
								.union(sch.attsFrom(en).stream().map(Object::toString).collect(Collectors.toList()),
										sch.fksFrom(en).stream().map(Object::toString).collect(Collectors.toList()))
								.toArray(new String[0]))));
			}
		}
	}
	return ret;
}
 
Example 10
Source File: CsvFileReader.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CsvFileReader( File file )
    throws IOException
{
    reader = new CSVReader( new FileReader( file ) );
    csvTable = reader.readAll();
}
 
Example 11
Source File: ApplicationConfiguration.java    From StatsAgg with Apache License 2.0 4 votes vote down vote up
private static List<GraphiteOutputModule> readGraphiteOutputModules() {
    
    List<GraphiteOutputModule> graphiteOutputModules = new ArrayList<>();
    
    for (int i = -1; i < 10000; i++) {
        String graphiteOutputModuleKey = "graphite_output_module_" + (i + 1);
        String graphiteOutputModuleValue = applicationConfiguration_.safeGetString(graphiteOutputModuleKey, null);
        
        if (graphiteOutputModuleValue == null) continue;
        
        try {
            CSVReader reader = new CSVReader(new StringReader(graphiteOutputModuleValue));
            List<String[]> csvValuesArray = reader.readAll();

            if ((csvValuesArray != null) && !csvValuesArray.isEmpty() && (csvValuesArray.get(0) != null)) {
                String[] csvValues = csvValuesArray.get(0);

                if (csvValues.length >= 4) {                                
                    boolean isOutputEnabled = Boolean.valueOf(csvValues[0]);
                    String host = csvValues[1];
                    int port = Integer.valueOf(csvValues[2]);
                    int numSendRetryAttempts = Integer.valueOf(csvValues[3]);
                    
                    int maxMetricsPerMessage = 1000;
                    if (csvValues.length > 4) maxMetricsPerMessage = Integer.valueOf(csvValues[4]);
                    
                    boolean sanitizeMetrics = false;
                    if (csvValues.length > 5) sanitizeMetrics = Boolean.valueOf(csvValues[5]);
                    
                    boolean substituteCharacters = false;
                    if (csvValues.length > 6) substituteCharacters = Boolean.valueOf(csvValues[6]);
                    
                    String uniqueId = "Graphite-" + (i+1);
                    
                    GraphiteOutputModule graphiteOutputModule = new GraphiteOutputModule(isOutputEnabled, host, port, 
                            numSendRetryAttempts, maxMetricsPerMessage, sanitizeMetrics, substituteCharacters, uniqueId);
                    
                    graphiteOutputModules.add(graphiteOutputModule);
                }
            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
        }
    }
    
    return graphiteOutputModules;
}
 
Example 12
Source File: ApplicationConfiguration.java    From StatsAgg with Apache License 2.0 4 votes vote down vote up
private static List<OpenTsdbTelnetOutputModule> readOpenTsdbTelnetOutputModules() {
    
    List<OpenTsdbTelnetOutputModule> openTsdbTelnetOutputModules = new ArrayList<>();
    
    for (int i = -1; i < 10000; i++) {
        String openTsdbTelnetOutputModuleKey = "opentsdb_telnet_output_module_" + (i + 1);
        String openTsdbTelnetOutputModuleValue = applicationConfiguration_.safeGetString(openTsdbTelnetOutputModuleKey, null);
        
        if (openTsdbTelnetOutputModuleValue == null) continue;
        
        try {
            CSVReader reader = new CSVReader(new StringReader(openTsdbTelnetOutputModuleValue));
            List<String[]> csvValuesArray = reader.readAll();

            if ((csvValuesArray != null) && !csvValuesArray.isEmpty() && (csvValuesArray.get(0) != null)) {
                String[] csvValues = csvValuesArray.get(0);

                if (csvValues.length >= 4) {                                
                    boolean isOutputEnabled = Boolean.valueOf(csvValues[0]);
                    String host = csvValues[1];
                    int port = Integer.valueOf(csvValues[2]);
                    int numSendRetryAttempts = Integer.valueOf(csvValues[3]);
                    
                    boolean sanitizeMetrics = false;
                    if (csvValues.length > 4) sanitizeMetrics = Boolean.valueOf(csvValues[4]);
                    
                    String uniqueId = "OpenTSDB-Telnet-" + (i+1);
                    
                    OpenTsdbTelnetOutputModule openTsdbTelnetOutputModule = new OpenTsdbTelnetOutputModule(isOutputEnabled, host, port, 
                            numSendRetryAttempts, sanitizeMetrics, uniqueId);
                    
                    openTsdbTelnetOutputModules.add(openTsdbTelnetOutputModule);
                }
            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
        }
    }
    
    return openTsdbTelnetOutputModules;
}
 
Example 13
Source File: ApplicationConfiguration.java    From StatsAgg with Apache License 2.0 4 votes vote down vote up
private static List<OpenTsdbHttpOutputModule> readOpenTsdbHttpOutputModules() {
    
    List<OpenTsdbHttpOutputModule> openTsdbHttpOutputModules = new ArrayList<>();
    
    for (int i = -1; i < 10000; i++) {
        String openTsdbHttpOutputModuleKey = "opentsdb_http_output_module_" + (i + 1);
        String openTsdbHttpOutputModuleValue = applicationConfiguration_.safeGetString(openTsdbHttpOutputModuleKey, null);
        
        if (openTsdbHttpOutputModuleValue == null) continue;
        
        try {
            CSVReader reader = new CSVReader(new StringReader(openTsdbHttpOutputModuleValue));
            List<String[]> csvValuesArray = reader.readAll();

            if ((csvValuesArray != null) && !csvValuesArray.isEmpty() && (csvValuesArray.get(0) != null)) {
                String[] csvValues = csvValuesArray.get(0);

                if (csvValues.length >= 4) {                                
                    boolean isOutputEnabled = Boolean.valueOf(csvValues[0]);
                    String url = csvValues[1];
                    int numSendRetryAttempts = Integer.valueOf(csvValues[2]);
                    int maxMetricsPerMessage = Integer.valueOf(csvValues[3]);
                    
                    boolean sanitizeMetrics = false;
                    if (csvValues.length > 4) sanitizeMetrics = Boolean.valueOf(csvValues[4]);
                    
                    String uniqueId = "OpenTSDB-HTTP-" + (i+1);
                    
                    OpenTsdbHttpOutputModule openTsdbHttpOutputModule = new OpenTsdbHttpOutputModule(isOutputEnabled, url, numSendRetryAttempts, 
                            maxMetricsPerMessage, sanitizeMetrics, uniqueId);
                    openTsdbHttpOutputModules.add(openTsdbHttpOutputModule);
                }
            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
        }
    }
    
    return openTsdbHttpOutputModules;
}