au.com.bytecode.opencsv.CSVReader Java Examples

The following examples show how to use au.com.bytecode.opencsv.CSVReader. 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: AbstractCsvReader.java    From the-app with Apache License 2.0 7 votes vote down vote up
public List<T> parseCsv() throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(getClassPathFilePath(), this.getClass().getClassLoader());
    InputStreamReader ioReader = new InputStreamReader(classPathResource.getInputStream(), "UTF-8");
    CSVReader reader = new CSVReader(ioReader, ';');

    ColumnPositionMappingStrategy<T> strat = new ColumnPositionMappingStrategy<>();
    strat.setType(getDestinationClass());
    strat.setColumnMapping(getColumnMapping());
    CsvToBean<T> csv = getParser();
    return csv.parse(strat, reader);
}
 
Example #2
Source File: ReportDataLoader.java    From adwords-alerting with Apache License 2.0 6 votes vote down vote up
/**
 * Generate ReportData from an input stream (normally an HTTP steam of report in CSV format),
 * which will be closed after reading.
 * @param stream the input stream (in CSV format)
 * @param clientCustomerId the client customer ID of this report
 * @return the generated ReportData
 */
public ReportData fromStream(InputStream stream, Long clientCustomerId) throws IOException {
  CSVReader csvReader = new CSVReader(new InputStreamReader(stream, Charset.defaultCharset()));
  String[] headerArray = csvReader.readNext();
  List<String[]> rowsArray = csvReader.readAll();
  csvReader.close();

  int rowsCount = rowsArray.size();
  List<List<String>> rows = new ArrayList<List<String>>(rowsCount);
  for (int i = 0; i < rowsCount; ++i) {
    // need to create a new ArrayList object which is extendible.
    List<String> row = new ArrayList<String>(Arrays.asList(rowsArray.get(i)));
    rows.add(row);
  }

  int columns = headerArray.length;
  List<String> columnNames = new ArrayList<String>(columns);
  for (int i = 0; i < columns; i++) {
    String fieldName = fieldsMapping.get(headerArray[i]);
    Preconditions.checkNotNull(fieldName, "Unknown field name: %s.", fieldName);
    columnNames.add(fieldName);
  }

  return new ReportData(clientCustomerId, reportType, columnNames, rows);
}
 
Example #3
Source File: OpenCSVParserExample.java    From journaldev with MIT License 6 votes vote down vote up
private static List<Employee> parseCSVFileLineByLine() throws IOException {
	//create CSVReader object
	CSVReader reader = new CSVReader(new FileReader("employees.csv"), ',');
	
	List<Employee> emps = new ArrayList<Employee>();
	//read line by line
	String[] record = null;
	//skip header row
	reader.readNext();
	
	while((record = reader.readNext()) != null){
		Employee emp = new Employee();
		emp.setId(record[0]);
		emp.setName(record[1]);
		emp.setRole(record[2]);
		emp.setSalary(record[3]);
		emps.add(emp);
	}
	
	reader.close();
	
	System.out.println(emps);
	return emps;
}
 
Example #4
Source File: transCorrelationQtl.java    From systemsgenetics with GNU General Public License v3.0 6 votes vote down vote up
public static PerChrIntervalTree<NamedGenomicRange> loadGeneMappings(final String geneMappingFile, final int window) throws IOException, NumberFormatException, Exception {
	CSVReader geneMapReader = new CSVReader(new InputStreamReader(new FileInputStream(geneMappingFile), ENCODING), '\t', '\0', 1);
	String[] nextLine;
	HashMap<String, ArrayList<NamedGenomicRange>> genes = new HashMap<>();
	while ((nextLine = geneMapReader.readNext()) != null) {
		String name = nextLine[0];
		String chr = nextLine[1];
		int start = Integer.valueOf(nextLine[2]);
		int stop = Integer.valueOf(nextLine[3]);

		ArrayList<NamedGenomicRange> chrGenes = genes.getOrDefault(chr, new ArrayList<>());
		chrGenes.add(new NamedGenomicRange(name, chr, start - window < 0 ? 0 : start - window, stop + window));
		genes.putIfAbsent(chr, chrGenes);

	}
	PerChrIntervalTree<NamedGenomicRange> geneMappings = new PerChrIntervalTree<>(NamedGenomicRange.class);
	for (Map.Entry<String, ArrayList<NamedGenomicRange>> entry : genes.entrySet()) {
		geneMappings.addChrElements(entry.getKey(), entry.getValue());
	}
	return geneMappings;
}
 
Example #5
Source File: MatchingGoldStandard.java    From winter with Apache License 2.0 6 votes vote down vote up
/**
 * Read all lines. Add positive and negative examples.
 *
 * @param reader
 * @throws IOException
 */
private void readAllLines(CSVReader reader) throws IOException {
	String[] values = null;

	while ((values = reader.readNext()) != null) {

		if (values.length == 3) {

			boolean isPositive = Boolean.parseBoolean(values[2]);

			Pair<String, String> example = new Pair<String, String>(
					values[0], values[1]);

			if (isPositive) {
				addPositiveExample(example);
			} else {
				addNegativeExample(example);
			}

		} else {
			logger.error(String.format("Skipping malformed line: %s",
					StringUtils.join(values,",")));
		}
	}
}
 
Example #6
Source File: CsvUtils.java    From pxf with Apache License 2.0 6 votes vote down vote up
/**
 * Get Table of data from CSV file
 *
 * @param pathToCsvFile to read from to Table
 * @return {@link Table} with data list from CSV file
 * @throws IOException
 */
public static Table getTable(String pathToCsvFile) throws IOException {

	// direct CSVReader to csv file
	CSVReader csvReader = new CSVReader(new FileReader(pathToCsvFile));

	// read csv file to List
	List<String[]> list = csvReader.readAll();

	// create table and load csv as list to it
	Table dataTable = new Table(pathToCsvFile, null);

	try {
		for (Iterator<String[]> iterator = list.iterator(); iterator.hasNext();) {
			dataTable.addRow(iterator.next());
		}
	} finally {
		csvReader.close();
	}

	return dataTable;
}
 
Example #7
Source File: YahooQuoteMessageConverter.java    From cloudstreetmarket.com with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected QuoteWrapper readInternal(Class<? extends QuoteWrapper> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
    CSVReader reader = new CSVReader(new InputStreamReader(httpInputMessage.getBody()));
    List<String[]> rows = reader.readAll();
    QuoteWrapper quoteWrapper = new QuoteWrapper();
    for (String[] row : rows) {
    	quoteWrapper.add(new YahooQuote(row[0], 
    								row[1], 
    								parseDouble(row[2]), 
    								parseDouble(row[3]), 
    								parseDouble(row[4]), 
    								parseDouble(row[5]), 
    								parsePercent(row[6]), 
    								parseDouble(row[7]), 
    								parseDouble(row[8]), 
    								parseDouble(row[9]), 
    								parseDouble(row[10]), 
    								parseInt(row[11]), 
    								row[12], 
    								row[13]));
    }

    return quoteWrapper;
}
 
Example #8
Source File: IpAddressDataManager.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestData(URI file, String datatype, Set<String> indexes) throws IOException {
    Assert.assertFalse("datatype has already been configured(" + datatype + ")", this.rawData.containsKey(datatype));
    try (final Reader reader = Files.newBufferedReader(Paths.get(file)); final CSVReader csv = new CSVReader(reader)) {
        String[] data;
        int count = 0;
        Set<RawData> ipData = new HashSet<>();
        while (null != (data = csv.readNext())) {
            final RawData raw = new IpAddrRawData(datatype, data);
            ipData.add(raw);
            count++;
        }
        this.rawData.put(datatype, ipData);
        this.rawDataIndex.put(datatype, indexes);
        log.info("ip address test data(" + file + ") count(" + count + ")");
    }
}
 
Example #9
Source File: CityDataManager.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestData(final URI file, final String datatype, final Set<String> indexes) throws IOException {
    Assert.assertFalse("datatype has already been configured(" + datatype + ")", this.rawData.containsKey(datatype));
    try (final Reader reader = Files.newBufferedReader(Paths.get(file)); final CSVReader csv = new CSVReader(reader, ',', '\"', '\0')) {
        String[] data;
        int count = 0;
        Set<RawData> cityData = new HashSet<>();
        while (null != (data = csv.readNext())) {
            final RawData raw = new CityRawData(datatype, data);
            cityData.add(raw);
            count++;
        }
        this.rawData.put(datatype, cityData);
        this.rawDataIndex.put(datatype, indexes);
        log.info("city test data(" + file + ") count(" + count + ")");
    }
}
 
Example #10
Source File: GroupsDataManager.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Override
public void addTestData(URI file, String datatype, Set<String> indexes) throws IOException {
    Assert.assertFalse("datatype has already been configured(" + datatype + ")", this.rawData.containsKey(datatype));
    try (final Reader reader = Files.newBufferedReader(Paths.get(file)); final CSVReader csv = new CSVReader(reader)) {
        String[] data;
        int count = 0;
        Set<RawData> entries = new HashSet<>();
        while (null != (data = csv.readNext())) {
            final RawData raw = new GroupRawData(datatype, data, GroupField.getMetadata());
            entries.add(raw);
            count++;
        }
        this.rawData.put(datatype, entries);
        this.rawDataIndex.put(datatype, indexes);
        log.info("groups test data(" + file + ") count(" + count + ")");
    }
}
 
Example #11
Source File: OpenCSVParserExample.java    From journaldev with MIT License 6 votes vote down vote up
private static void parseCSVFileAsList() throws IOException {
	//create CSVReader object
	CSVReader reader = new CSVReader(new FileReader("employees.csv"), ',');

	List<Employee> emps = new ArrayList<Employee>();
	//read all lines at once
	List<String[]> records = reader.readAll();
	
	Iterator<String[]> iterator = records.iterator();
	//skip header row
	iterator.next();
	
	while(iterator.hasNext()){
		String[] record = iterator.next();
		Employee emp = new Employee();
		emp.setId(record[0]);
		emp.setName(record[1]);
		emp.setRole(record[2]);
		emp.setSalary(record[3]);
		emps.add(emp);
	}
	
	reader.close();
	
	System.out.println(emps);
}
 
Example #12
Source File: Utils.java    From datasync with MIT License 6 votes vote down vote up
/**
 * Reads first line of the given file after skipping 'skip' lines and returns it's contents as a string array.
 */
public static String[] pullHeadersFromFile(File fileToPublish, FileTypeControl fileControl, int skip)
        throws IOException {

    CSVReader reader = getReader(fileToPublish, fileControl);

    int linesRead = 0;
    String[] nextRecord;
    Charset charset = getCharset(fileControl);
    while ((nextRecord = reader.readNext()) != null && linesRead++ < skip) {}
    byte[] bom = BOM.getBytes(charset);
    if (nextRecord != null && nextRecord.length > 0) {
        byte[] firstStringBytes = nextRecord[0].getBytes(charset);
        if (startsWith(bom, firstStringBytes))
            nextRecord[0] = nextRecord[0].substring(1);
    }
    reader.close();
    return nextRecord;
}
 
Example #13
Source File: SplunkConnectionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static void parseResults(InputStream in, SearchResultListener srl) {
  try (CSVReader r = new CSVReader(
      new BufferedReader(
          new InputStreamReader(in, StandardCharsets.UTF_8)))) {
    String[] header = r.readNext();
    if (header != null
        && header.length > 0
        && !(header.length == 1 && header[0].isEmpty())) {
      srl.setFieldNames(header);

      String[] line;
      while ((line = r.readNext()) != null) {
        if (line.length == header.length) {
          srl.processSearchResult(line);
        }
      }
    }
  } catch (IOException ignore) {
    StringWriter sw = new StringWriter();
    ignore.printStackTrace(new PrintWriter(sw));
    LOGGER.warn("{}\n{}", ignore.getMessage(), sw);
  }
}
 
Example #14
Source File: ProduceCsvFiles.java    From collect-earth with MIT License 6 votes vote down vote up
private void processHeaders(File fileToDivide) throws IOException {
	
	String[] firstRow = null;
	// longitude has to be a number, otherwise it is a header
	try (
			CSVReader reader = CsvReaderUtils.getCsvReader(fileToDivide.getPath());
	){	

		firstRow = reader.readNext();
		int numberOfIdColumns = getNumberOfIDColumns();
		Float.parseFloat( firstRow[ numberOfIdColumns+1 ]); // The first column after the ID column(s) should be a real number

	} catch (NumberFormatException e) {
		logger.warn("There are no numbers in the third row of the CSV file where the latitude should be" );
		setHeaders( firstRow);
	}
}
 
Example #15
Source File: CSVModel.java    From datasync with MIT License 6 votes vote down vote up
private CSVReader getCSVReader(ControlFile controlFile, int skip) throws IOException{
    String path = controlFile.getFileTypeControl().filePath;
    String encoding = controlFile.getFileTypeControl().encoding;
    char sep = controlFile.getFileTypeControl().separator.charAt(0);
    char quote = controlFile.getFileTypeControl().quote.charAt(0);
    char escape = '\u0000';

    InputStreamReader inputReader = new InputStreamReader(new FileInputStream(controlFile.getFileTypeControl().filePath), controlFile.getFileTypeControl().encoding);

    if (controlFile.getFileTypeControl().escape != null ){
        if (controlFile.getFileTypeControl().escape.equals(""))
            escape = '\u0000';
        else
            escape = controlFile.getFileTypeControl().escape.charAt(0);
    }
    CSVReader reader = new CSVReader(inputReader,
            sep,
            quote,
            escape,
            skip);

    return reader;
}
 
Example #16
Source File: CSVReaderService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read a CSV file and call the method {@link #readLineOfCSVFile(String[], int, Locale, String) readLineOfCSVFile} for each of its lines.
 * 
 * @param strPath
 *            Path if the file to read in the file system.
 * @param nColumnNumber
 *            Number of columns of each lines. Use 0 to skip column number check (for example if every lines don't have the same number of columns)
 * @param bCheckFileBeforeProcessing
 *            Indicates if the file should be check before processing any of its line. If it is set to true, then then no line is processed if the file has
 *            any error.
 * @param bExitOnError
 *            Indicates if the processing of the CSV file should end on the first error, or at the end of the file.
 * @param bSkipFirstLine
 *            Indicates if the first line of the file should be skipped or not.
 * @param locale
 *            the locale
 * @param strBaseUrl
 *            The base URL
 * @return Returns the list of errors that occurred during the processing of the file. The returned list is sorted
 * @see CSVMessageDescriptor#compareTo(CSVMessageDescriptor) CSVMessageDescriptor.compareTo(CSVMessageDescriptor) for information about sort
 */
public List<CSVMessageDescriptor> readCSVFile( String strPath, int nColumnNumber, boolean bCheckFileBeforeProcessing, boolean bExitOnError,
        boolean bSkipFirstLine, Locale locale, String strBaseUrl )
{
    java.io.File file = new java.io.File( strPath );

    try ( FileReader fileReader = new FileReader( file ) )
    {
        CSVReader csvReader = new CSVReader( fileReader, getCSVSeparator( ), getCSVEscapeCharacter( ) );

        return readCSVFile( fileReader, csvReader, nColumnNumber, bCheckFileBeforeProcessing, bExitOnError, bSkipFirstLine, locale, strBaseUrl );
    }
    catch( IOException e )
    {
        AppLogService.error( e.getMessage( ), e );
    }

    List<CSVMessageDescriptor> listErrors = new ArrayList<>( );
    CSVMessageDescriptor errorDescription = new CSVMessageDescriptor( CSVMessageLevel.ERROR, 0,
            I18nService.getLocalizedString( MESSAGE_NO_FILE_FOUND, locale ) );
    listErrors.add( errorDescription );

    return listErrors;
}
 
Example #17
Source File: WanWrapper.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
public  String[][] getHeader(String filename) throws IOException {
  CSVReader reader = new CSVReader(new FileReader(filename));
  String[] data =  reader.readNext();
  if (data == null)
    return null;
  String[][] headers = new String[4][data.length];
  headers[0]=data;
  headers[1]= reader.readNext();
  headers[2]= reader.readNext();
  headers[3]= reader.readNext();
  if (headers[0]==null||headers[1]==null ||headers[2]==null||headers[3]==null) {
    logger.debug("Header read incompletely.");
    logger.debug(""+(headers[0]==null));
    logger.debug(""+(headers[1]==null));
    logger.debug(""+(headers[2]==null));
    logger.debug(""+(headers[3]==null));
    return null;
  }
  reader.close();
  return headers;
}
 
Example #18
Source File: GetFusionCsv.java    From collect-earth with MIT License 6 votes vote down vote up
private void processFile() throws IOException {
	final CSVReader csvReader = new CSVReader(new FileReader(new File("ullaan.csv")), ';');
	final CSVWriter csvWriter = new CSVWriter(new FileWriter(new File("resultFusion.csv")), ';');
	String[] nextRow;
	final String[] writeRow = new String[4];
	writeRow[0] = "Coordinates";
	writeRow[1] = "Land Use ID";
	writeRow[2] = "Land Use name";
	writeRow[3] = "Placemark ID";
	csvWriter.writeNext(writeRow);
	while ((nextRow = csvReader.readNext()) != null) {

		writeRow[0] = "<Point><coordinates>" + replaceComma(nextRow[2]) + "," + replaceComma(nextRow[3]) + ",0.0</coordinates></Point>";
		final String landUse = nextRow[5];
		final int classId = getId(landUse);
		writeRow[1] = classId + "";
		writeRow[2] = landUse;
		writeRow[3] = nextRow[0];
		csvWriter.writeNext(writeRow);
	}
	csvWriter.close();
	csvReader.close();
}
 
Example #19
Source File: CSVConfig.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private String[] getHeader() throws IOException, DataServiceFault {
    if (!this.isHasHeader()) {
        return null;
    }

    CSVReader reader = null;
    try {
        reader = this.createCSVReader(this.getHeaderRow() - 1);
        return reader.readNext();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("Error in closing CSV reader", e);
            }
        }
    }
}
 
Example #20
Source File: CSVDataSource.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private TupleReader(File csvfile) {
  try {
    in = (encoding == null)
      ? new InputStreamReader(new FileInputStream(csvfile))
      : new InputStreamReader(new FileInputStream(csvfile), encoding);
    this.reader = new CSVReader(in, separator, quoteCharacter);
    // ignore first N lines
    for (int i=0; i < ignoreFirstLines; i++) {
      readNext();
    }
  } catch (Throwable e) {
    throw new OntopiaRuntimeException(e);
  }
}
 
Example #21
Source File: TeamCity.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
public Result getResultsFromCSV(String filePath) {
    Result results = new Result();
    try {
        CSVReader reader = new CSVReader(new FileReader(new File(filePath)));
        List<String[]> lines = reader.readAll();
        for (String[] test : lines.subList(1, lines.size())) {
            results.addResult(test[1], test[2]);
        }
    } catch (Exception e) {/**/}
    return results;
}
 
Example #22
Source File: CSVSerializer.java    From data-prep with Apache License 2.0 5 votes vote down vote up
/**
 * Write the line content.
 *
 * @param reader the csv reader to use as data source.
 * @param metadata the dataset metadata to use to get the columns.
 * @param generator the json generator used to actually write the line content.
 * @param separator the csv separator to use.
 * @param limit The maximum number of lines in the exported content.
 * @throws IOException if an error occurs.
 */
private void writeLineContent(CSVReader reader, DataSetMetadata metadata, JsonGenerator generator, String separator,
        long limit) throws IOException {
    String[] line;
    int current = 0;

    while ((line = reader.readNext()) != null && withinLimit(limit, current)) {
        // skip empty lines
        if (line.length == 1 && (StringUtils.isEmpty(line[0]) || line[0].charAt(0) == Character.MIN_VALUE)) {
            continue;
        }

        List<ColumnMetadata> columns = metadata.getRowMetadata().getColumns();
        generator.writeStartObject();
        int columnsSize = columns.size();
        for (int i = 0; i < columnsSize; i++) {
            ColumnMetadata columnMetadata = columns.get(i);

            generator.writeFieldName(columnMetadata.getId());

            // deal with additional content (line.length > columns.size)
            if (i == columnsSize - 1 && line.length > columnsSize) {
                String additionalContent = getRemainingColumns(line, i, separator);
                generator.writeString(cleanCharacters(additionalContent));
            }
            // deal with fewer content (line.length < columns.size)
            else if (i < line.length && line[i] != null) {
                generator.writeString(cleanCharacters(line[i]));
            }
            // deal with null
            else {
                generator.writeNull();
            }
        }
        generator.writeEndObject();
        current++;
    }
}
 
Example #23
Source File: YahooIntraDayHistoMessageConverter.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected QuoteWrapper readInternal(Class<? extends QuoteWrapper> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
    CSVReader reader = new CSVReader(new InputStreamReader(httpInputMessage.getBody()));
    List<String[]> rows = reader.readAll();
    QuoteWrapper quoteWrapper = new QuoteWrapper();
    for (String[] row : rows) {
    	
    	try{
    		Integer.valueOf(row[0]);
    	}
    	catch(NumberFormatException e){
    		break;
    	}

    	quoteWrapper.add(new YahooQuote(row[0], 
    								row[1], 
    								parseDouble(row[2]), 
    								parseDouble(row[3]), 
    								parseDouble(row[4]), 
    								parseDouble(row[5]), 
    								parsePercent(row[6]), 
    								parseDouble(row[7]), 
    								parseDouble(row[8]), 
    								parseDouble(row[9]), 
    								parseDouble(row[10]), 
    								parseInt(row[11]), 
    								row[12], 
    								row[13]));
    }

    return quoteWrapper;
}
 
Example #24
Source File: SasFileReaderUnitTest.java    From parso with Apache License 2.0 5 votes vote down vote up
private void closeCSVReader(CSVReader csvReader) {
    try {
        if (csvReader != null) {
            csvReader.close();
        }
    } catch (IOException ignore) {
    }
}
 
Example #25
Source File: LogicParser.java    From barchomat with GNU General Public License v2.0 5 votes vote down vote up
static ArrayList<Logic.Data> loadLogicFile(InputStream in) throws IOException {
    CSVReader reader = new CSVReader(new InputStreamReader(in));
    List<String[]> lines = reader.readAll();
    String[] header = lines.get(0);
    Logic.Data.Type[] types = parseTypes(lines.get(1));
    if (!NAME_KEY.equals(header[0]) || types[0] != Logic.Data.Type.STRING) {
        log.debug("Forcing Name column");
        header[0] = NAME_KEY;
        types[0] = Logic.Data.Type.STRING;
    }

    ArrayList<Logic.Data> logic = new ArrayList<>();

    Logic.Data data = null;
    for (int i = 2; i < lines.size(); i++) {
        String[] line = lines.get(i);
        if (!isNullOrEmpty(line[0])) {
            data = new Logic.Data(header);
            logic.add(data);
        } else if (data == null) {
            throw new ResourceException("Sub-header not found");
        }
        data.addLine(parseValues(types, line));
    }

    return logic;
}
 
Example #26
Source File: SasFileReaderUnitTest.java    From parso with Apache License 2.0 5 votes vote down vote up
@Test
public void testData() {
    long programStart = System.currentTimeMillis();
    InputStream fileInputStream = getResourceAsStream(fileName);
    logger.info("Processing file {}", fileName);
    Writer writer = new StringWriter();
    InputStreamReader inputStreamReader = new InputStreamReader(
            getResourceAsStream(fileName.toLowerCase().replace("sas7bdat", "csv")));
    try {
        SasFileReader sasFileReader = new SasFileReaderImpl(fileInputStream);
        long rowCount = sasFileReader.getSasFileProperties().getRowCount();
        List<Column> columns = sasFileReader.getColumns();
        CSVReader controlReader = new CSVReader(inputStreamReader);
        CSVDataWriter csvDataWriter = new CSVDataWriterImpl(writer, ",", "\n", Locale.UK);
        controlReader.readNext();
        for (int i = 0; i < rowCount; i++) {
            csvDataWriter.writeRow(sasFileReader.getColumns(), sasFileReader.readNext());
            if (i != 0 && i % COMPARE_ROWS_COUNT == 0) {
                compareResultWithControl(controlReader, writer, i - COMPARE_ROWS_COUNT, columns);
                ((StringWriter) writer).getBuffer().setLength(0);
            }
        }
        compareResultWithControl(controlReader, writer, (int) (rowCount - rowCount % COMPARE_ROWS_COUNT), columns);
        assertThat(controlReader.readNext()).isNull();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        closeWriter(writer);
        closeInputStream(fileInputStream);
        closeInputStreamReader(inputStreamReader);
    }
    logger.info("Time passed: {} ms", System.currentTimeMillis() - programStart);
}
 
Example #27
Source File: HackLabelEdgesClient.java    From render with GNU General Public License v2.0 5 votes vote down vote up
private Map<Double, List<TileEdge>> parseTileEdgeCsvFile(final String csvFilePath)
        throws IOException {

    LOG.info("parseTileEdgeCsvFile: parsing {}", csvFilePath);

    int edgeCount = 0;

    final Map<Double, List<TileEdge>> zToEdgeList = new HashMap<>();
    try (final CSVReader csvReader = new CSVReader(new FileReader(csvFilePath))) {
        String[] values;
        while ((values = csvReader.readNext()) != null) {
            if (values.length == 3) {
                final String zString = values[0].trim();
                final String positionString = values[2].trim().toUpperCase();
                if ((zString.length() > 0) && (Character.isDigit(zString.charAt(0)))) {
                    final Double z = Double.parseDouble(zString);
                    final List<TileEdge> tileEdgeList = zToEdgeList.computeIfAbsent(z, list -> new ArrayList<>());
                    final TileEdgePosition position;
                    try {
                        position = TileEdgePosition.valueOf(positionString);
                    } catch (final Throwable t) {
                        throw new IllegalArgumentException("failed to parse tile edge position '" + positionString + "'", t);
                    }
                    tileEdgeList.add(new TileEdge(values[1], position));
                    edgeCount++;
                }
            }
        }
    }

    LOG.info("parseTileEdgeCsvFile: loaded {} edges from {}", edgeCount, csvFilePath);

    return zToEdgeList;
}
 
Example #28
Source File: CSVModel.java    From datasync with MIT License 5 votes vote down vote up
private void updateColumnNames(ControlFile file) throws IOException {
    boolean hasHeaderRow = file.getFileTypeControl().hasHeaderRow;
    CSVReader headerReader = getCSVReader(file, 0);
    String[] row = headerReader.readNext();

    if (hasHeaderRow) {
        columnNames = row;
    }
    else{
        columnNames = generatePlaceholderNames(row.length);
    }
    fireTableStructureChanged();
}
 
Example #29
Source File: CreateLocationDb.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
@Override
@Test(enabled = false)
public void init() throws Exception {
  String buildDirectory = System.getProperty("buildDirectory", ".");
  if (buildDirectory == null)
    buildDirectory = ".";

  databaseDocumentTx = new ODatabaseDocumentTx("plocal:" + buildDirectory + "/location");
  if (databaseDocumentTx.exists()) {
    databaseDocumentTx.open("admin", "admin");
    databaseDocumentTx.drop();
  }

  databaseDocumentTx.create();
  OSchema schema = databaseDocumentTx.getMetadata().getSchema();
  OClass oClass = schema.createClass("City");
  oClass.createProperty("latitude", OType.DOUBLE);
  oClass.createProperty("longitude", OType.DOUBLE);
  oClass.createProperty("name", OType.STRING);
  oClass.createIndex("City.latitude_longitude", "SPATIAL", null, null, "LUCENE", new String[] { "latitude", "longitude" });
  oClass.createIndex("City.name", "FULLTEXT", null, null, "LUCENE", new String[] { "name" });
  ZipFile zipFile = new ZipFile("files/location.csv.zip");
  Enumeration<? extends ZipEntry> entries = zipFile.entries();

  while (entries.hasMoreElements()) {

    ZipEntry entry = entries.nextElement();
    if (entry.getName().equals("location.csv")) {

      InputStream stream = zipFile.getInputStream(entry);
      reader = new CSVReader(new InputStreamReader(stream), ',');
    }
  }

}
 
Example #30
Source File: CsvServiceImpl.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public List<String[]> buildLinesFromFile(File file) throws IOException {
  CSVReader reader =
      createCsvReader(file.getName(), removeByteOrderMark(new FileInputStream(file)));
  List<String[]> content = reader.readAll();
  validateCsvFile(content, file.getName());
  return content;
}