• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java CSVReaderBuilder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.opencsv.CSVReaderBuilder的典型用法代码示例。如果您正苦于以下问题:Java CSVReaderBuilder类的具体用法?Java CSVReaderBuilder怎么用?Java CSVReaderBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



CSVReaderBuilder类属于com.opencsv包,在下文中一共展示了CSVReaderBuilder类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: parse

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
@Override
public Stream<Row> parse(FileSource source) {
	InputStreamReader csvStreamReader = new InputStreamReader(source.toInputStream(), csvParserConfiguration.getCharset());
	Iterator<String[]> csvRowIterator = new CSVReaderBuilder(csvStreamReader)
		.withCSVParser(
			new CSVParserBuilder()
				.withEscapeChar(csvParserConfiguration.getEscapeChar())
				.withFieldAsNull(csvParserConfiguration.getNullFieldIndicator())
				.withIgnoreLeadingWhiteSpace(csvParserConfiguration.isIgnoreLeadingWhiteSpace())
				.withIgnoreQuotations(csvParserConfiguration.isIgnoreQuotations())
				.withQuoteChar(csvParserConfiguration.getQuoteChar())
				.withSeparator(csvParserConfiguration.getSeparator())
				.withStrictQuotes(csvParserConfiguration.isStrictQuotes())
				.build()
		)
		.withKeepCarriageReturn(csvParserConfiguration.isKeepCr())
		.build()
		.iterator();

	return IteratorStreams
		.stream(new CsvRowIterator(csvRowIterator))
		.onClose(() -> IteratorStreams.close(csvStreamReader));
}
 
开发者ID:Coreoz,项目名称:Windmill,代码行数:24,代码来源:CsvParser.java


示例2: getHourlyData

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
private static TimeSeries getHourlyData() throws IOException {
	final String path = "./fortran_benchmark/hourly_stl_test.csv";

	CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(path));

	TimeSeries ts = new TimeSeries();

	try (CSVReader reader = builder.build()) {

		String[] nextLine;
		long time = 1492457959000L;
		while ((nextLine = reader.readNext()) != null) {
			ts.times.add(time);
			time += 3600 * 1000;
			double value = Double.parseDouble(nextLine[0]);
			ts.values.add(value);
		}
	}
	return ts;
}
 
开发者ID:ServiceNow,项目名称:stl-decomp-4j,代码行数:21,代码来源:StlPerfTest.java


示例3: getCo2Data

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
@SuppressWarnings("Duplicates")
private static TimeSeries getCo2Data() throws IOException {

	final String path =  "../StlDemoRestServer/co2.csv";

	CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(path));

	TimeSeries ts = new TimeSeries();

	try (CSVReader reader = builder.withSkipLines(1).build()) {

		String[] nextLine;
		while ((nextLine = reader.readNext()) != null) {
			double dateAsYear = Double.parseDouble(nextLine[1]);
			long time = (long) ((dateAsYear - 1970.0) * 365.25 * 24 * 60 * 60 * 1000);
			ts.times.add(time);

			double value = Double.parseDouble(nextLine[2]);
			ts.values.add(value);
		}
	}
	return ts;
}
 
开发者ID:ServiceNow,项目名称:stl-decomp-4j,代码行数:24,代码来源:StlPerfTest.java


示例4: getTimeSeries

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
@SuppressWarnings("Duplicates")
public static TimeSeries getTimeSeries(String fileName) throws IOException {
	CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(fileName));

	TimeSeries ts = new TimeSeries();

	try (CSVReader reader = builder.withSkipLines(1).build()) {

		String[] nextLine;
		while ((nextLine = reader.readNext()) != null) {
			double dateAsYear = Double.parseDouble(nextLine[1]);
			long time = (long) ((dateAsYear - 1970.0) * 365.25 * 24 * 60 * 60 * 1000);
			ts.times.add(time);

			double value = Double.parseDouble(nextLine[2]);
			ts.values.add(value);
		}
	}
	return ts;
}
 
开发者ID:ServiceNow,项目名称:stl-decomp-4j,代码行数:21,代码来源:StlDemoRestServer.java


示例5: configure

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
@Override
protected void configure(InputStream inputStream, Map<String, String> metadata, final Long lastOffset) throws IOException {
  log.trace("configure() - creating csvParser");
  this.csvParser = this.config.createCSVParserBuilder().build();
  this.streamReader = new InputStreamReader(inputStream, this.config.charset);
  CSVReaderBuilder csvReaderBuilder = this.config.createCSVReaderBuilder(this.streamReader, csvParser);
  this.csvReader = csvReaderBuilder.build();

  String[] fieldNames;

  if (this.config.firstRowAsHeader) {
    log.trace("configure() - Reading the header row.");
    fieldNames = this.csvReader.readNext();
    log.info("configure() - field names from header row. fields = {}", Joiner.on(", ").join(fieldNames));
  } else {
    log.trace("configure() - Using fields from schema {}", this.config.valueSchema.name());
    fieldNames = new String[this.config.valueSchema.fields().size()];
    int index = 0;
    for (Field field : this.config.valueSchema.fields()) {
      fieldNames[index++] = field.name();
    }
    log.info("configure() - field names from schema order. fields = {}", Joiner.on(", ").join(fieldNames));
  }

  if (null != lastOffset) {
    log.info("Found previous offset. Skipping {} line(s).", lastOffset.intValue());
    String[] row = null;
    while (null != (row = this.csvReader.readNext()) && this.csvReader.getLinesRead() < lastOffset) {
      log.trace("skipped row");
    }
  }

  this.fieldNames = fieldNames;
  this.fileMetadata = metadata;
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-spooldir,代码行数:36,代码来源:SpoolDirCsvSourceTask.java


示例6: createCSVReaderBuilder

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
public CSVReaderBuilder createCSVReaderBuilder(Reader reader, CSVParser parser) {
  return new CSVReaderBuilder(reader)
      .withCSVParser(parser)
      .withKeepCarriageReturn(this.keepCarriageReturn)
      .withSkipLines(this.skipLines)
      .withVerifyReader(this.verifyReader)
      .withFieldAsNull(nullFieldIndicator);
}
 
开发者ID:jcustenborder,项目名称:kafka-connect-spooldir,代码行数:9,代码来源:SpoolDirCsvSourceConnectorConfig.java


示例7: openInputData

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
private CSVReader openInputData() throws FileNotFoundException, UnsupportedEncodingException {
    return new CSVReaderBuilder(new InputStreamReader(new FileInputStream(dataInPath + scenarioName + "." + CSV_TYPE), DEFAULT_ENDODING))
            .withCSVParser(new CSVParserBuilder().withSeparator(CSV_CHAR_SEPARATOR).build()).build();
}
 
开发者ID:NoraUi,项目名称:NoraUi,代码行数:5,代码来源:CsvDataProvider.java


示例8: openOutputData

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
private CSVReader openOutputData() throws FileNotFoundException, UnsupportedEncodingException {
    return new CSVReaderBuilder(new InputStreamReader(new FileInputStream(dataOutPath + scenarioName + "." + CSV_TYPE), DEFAULT_ENDODING))
            .withCSVParser(new CSVParserBuilder().withSeparator(CSV_CHAR_SEPARATOR).build()).build();
}
 
开发者ID:NoraUi,项目名称:NoraUi,代码行数:5,代码来源:CsvDataProvider.java


示例9: read

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
public static Table read(CsvReadOptions options) throws IOException {
    ColumnType[] types = options.columnTypes();
    byte[] bytes = options.reader() != null
        ? CharStreams.toString(options.reader()).getBytes() : null;
    if (types == null) {
      InputStream detectTypesStream = options.reader() != null
          ? new ByteArrayInputStream(bytes)
          : new FileInputStream(options.file());
      types = detectColumnTypes(detectTypesStream, options.header(), options.separator(), options.sample()); 
    }

    // All other read methods end up here, make sure we don't have leading Unicode BOM
    InputStream stream = options.reader() != null
        ? new ByteArrayInputStream(bytes)
        : new FileInputStream(options.file());
    UnicodeBOMInputStream ubis = new UnicodeBOMInputStream(stream);
    ubis.skipBOM();

    Table table;
    CSVParser csvParser = new CSVParserBuilder()
            .withSeparator(options.separator())
            .build();

    try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(ubis)).withCSVParser(csvParser).build()) {
        String[] nextLine;
        String[] columnNames;
        List<String> headerRow;
        if (options.header()) {
            nextLine = reader.readNext();
            headerRow = Lists.newArrayList(nextLine);
            columnNames = selectColumnNames(headerRow, types);
        } else {
            columnNames = makeColumnNames(types);
            headerRow = Lists.newArrayList(columnNames);
        }

        table = Table.create(options.tableName());
        cleanNames(headerRow);
        for (int x = 0; x < types.length; x++) {
            if (types[x] != SKIP) {
                String columnName = headerRow.get(x);
                if (Strings.isNullOrEmpty(columnName)) {
                    columnName = "Column " + table.columnCount();
                }
                Column newColumn = TypeUtils.newColumn(columnName, types[x]);
                table.addColumn(newColumn);
            }
        }
        int[] columnIndexes = new int[columnNames.length];
        for (int i = 0; i < columnIndexes.length; i++) {
            // get the index in the original table, which includes skipped fields
            columnIndexes[i] = headerRow.indexOf(columnNames[i]);
        }
        // Add the rows
        long rowNumber = options.header() ? 1L : 0L;
        while ((nextLine = reader.readNext()) != null) {
            // for each column that we're including (not skipping)
            int cellIndex = 0;
            for (int columnIndex : columnIndexes) {
                Column column = table.column(cellIndex);
                try {
                    column.appendCell(nextLine[columnIndex]);
                } catch (Exception e) {
                    throw new AddCellToColumnException(e, columnIndex, rowNumber, columnNames, nextLine);
                }
                cellIndex++;
            }
            rowNumber++;
        }
    }
    return table;
}
 
开发者ID:jtablesaw,项目名称:tablesaw,代码行数:73,代码来源:CsvReader.java


示例10: headerOnly

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
/**
 * Returns a Table constructed from a CSV File with the given file name
 * <p>
 * The @code{fileName} is used as the initial table name for the new table
 *
 * @param types           An array of the types of columns in the file, in the order they appear
 * @param header          Is the first row in the file a header?
 * @param columnSeparator the delimiter
 * @param file        The fully specified file name. It is used to provide a default name for the table
 * @return A Relation containing the data in the csv file.
 * @throws IOException if file cannot be read
 */
public static Table headerOnly(ColumnType types[], boolean header, char columnSeparator, File file)
        throws IOException {

    FileInputStream fis = new FileInputStream(file);       
    // make sure we don't have leading Unicode BOM
    UnicodeBOMInputStream ubis = new UnicodeBOMInputStream(fis);
    ubis.skipBOM();
    
    Reader reader = new InputStreamReader(ubis);
    BufferedReader streamReader = new BufferedReader(reader);

    Table table;
    CSVParser csvParser = new CSVParserBuilder()
            .withSeparator(columnSeparator)
            .build();
    try (CSVReader csvReader = new CSVReaderBuilder(streamReader).withCSVParser(csvParser).build()) {

        String[] nextLine;
        String[] columnNames;
        List<String> headerRow;
        if (header) {
            nextLine = csvReader.readNext();
            headerRow = Lists.newArrayList(nextLine);
            columnNames = selectColumnNames(headerRow, types);
        } else {
            columnNames = makeColumnNames(types);
            headerRow = Lists.newArrayList(columnNames);
        }

        table = Table.create(file.getName());
        for (int x = 0; x < types.length; x++) {
            if (types[x] != SKIP) {
                Column newColumn = TypeUtils.newColumn(headerRow.get(x).trim(), types[x]);
                table.addColumn(newColumn);
            }
        }
        int[] columnIndexes = new int[columnNames.length];
        for (int i = 0; i < columnIndexes.length; i++) {
            // get the index in the original table, which includes skipped fields
            columnIndexes[i] = headerRow.indexOf(columnNames[i]);
        }
    }
    return table;
}
 
开发者ID:jtablesaw,项目名称:tablesaw,代码行数:57,代码来源:CsvReader.java


示例11: detectColumnTypes

import com.opencsv.CSVReaderBuilder; //导入依赖的package包/类
/**
 * Estimates and returns the type for each column in the delimited text file {@code file}
 * <p>
 * The type is determined by checking a sample of the data in the file. Because only a sample of the data is
 * checked,
 * the types may be incorrect. If that is the case a Parse Exception will be thrown.
 * <p>
 * The method {@code printColumnTypes()} can be used to print a list of the detected columns that can be
 * corrected and
 * used to explicitly specify the correct column types.
 */
protected static ColumnType[] detectColumnTypes(InputStream stream, boolean header, char delimiter, boolean skipSampling)
        throws IOException {

    int linesToSkip = header ? 1 : 0;

    // to hold the results
    List<ColumnType> columnTypes = new ArrayList<>();

    // to hold the data read from the file
    List<List<String>> columnData = new ArrayList<>();

    int rowCount = 0; // make sure we don't go over maxRows

    // make sure we don't have leading Unicode BOM
    UnicodeBOMInputStream ubis = new UnicodeBOMInputStream(stream);
    ubis.skipBOM();

    CSVParser csvParser = new CSVParserBuilder()
            .withSeparator(delimiter)
            .build();
    try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(ubis))
            .withCSVParser(csvParser)
            .withSkipLines(linesToSkip)
            .build()) {
        String[] nextLine;
        int nextRow = 0;
        while ((nextLine = reader.readNext()) != null) {
            // initialize the arrays to hold the strings. we don't know how many we need until we read the first row
            if (rowCount == 0) {
                for (int j = 0; j < nextLine.length; j++) {
                    columnData.add(new ArrayList<>());
                }
            }
            int columnNumber = 0;
            if (rowCount == nextRow) {
                for (String field : nextLine) {
                    columnData.get(columnNumber).add(field);
                    columnNumber++;
                }
            }
            if (rowCount == nextRow) {
                if (skipSampling) {
                    nextRow = nextRowWithoutSampling(nextRow);
                } else {
                    nextRow = nextRow(nextRow);
                }
            }
            rowCount++;
        }
    }

    // now detect
    for (List<String> valuesList : columnData) {
        ColumnType detectedType = detectType(valuesList);
        columnTypes.add(detectedType);
    }
    return columnTypes.toArray(new ColumnType[columnTypes.size()]);
}
 
开发者ID:jtablesaw,项目名称:tablesaw,代码行数:70,代码来源:CsvReader.java



注:本文中的com.opencsv.CSVReaderBuilder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java MemberValuePair类代码示例发布时间:2022-05-23
下一篇:
Java Version类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap