本文整理汇总了Java中au.com.bytecode.opencsv.CSVReader类的典型用法代码示例。如果您正苦于以下问题:Java CSVReader类的具体用法?Java CSVReader怎么用?Java CSVReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CSVReader类属于au.com.bytecode.opencsv包,在下文中一共展示了CSVReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: TableDataIterable
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
/**
* Constructor
*
* @param catalog_tbl
* @param table_file
* @param has_header
* whether we expect the data file to include a header in the
* first row
* @param auto_generate_first_column
* TODO
* @throws Exception
*/
public TableDataIterable(Table catalog_tbl, File table_file, boolean has_header, boolean auto_generate_first_column) throws Exception {
this.catalog_tbl = catalog_tbl;
this.table_file = table_file;
this.auto_generate_first_column = auto_generate_first_column;
this.reader = new CSVReader(FileUtil.getReader(this.table_file));
// Throw away the first row if there is a header
if (has_header) {
this.reader.readNext();
this.line_ctr++;
}
// Column Types + Foreign Keys
// Determine whether the column references a foreign key, and thus will
// need to be converted to an integer field later
this.types = new VoltType[catalog_tbl.getColumns().size()];
this.fkeys = new boolean[this.types.length];
this.nullable = new boolean[this.types.length];
for (Column catalog_col : catalog_tbl.getColumns()) {
int idx = catalog_col.getIndex();
this.types[idx] = VoltType.get((byte) catalog_col.getType());
this.fkeys[idx] = (CatalogUtil.getForeignKeyParent(catalog_col) != null);
this.nullable[idx] = catalog_col.getNullable();
} // FOR
}
示例2: TestFileIterator
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public TestFileIterator(String relationName, Reader reader, char separator, char quotechar,
char escape,
int skipLines, boolean strictQuotes, boolean ignoreLeadingWhiteSpace,
boolean hasHeader, boolean skipDifferingLines)
throws InputIterationException {
this.relationName = relationName;
this.csvReader =
new CSVReader(reader, separator, quotechar, escape, skipLines, strictQuotes,
ignoreLeadingWhiteSpace);
this.skipDifferingLines = skipDifferingLines;
this.nextLine = readNextLine();
if (this.nextLine != null) {
this.numberOfColumns = this.nextLine.size();
}
if (hasHeader) {
this.headerLine = this.nextLine;
next();
}
// If the header is still null generate a standard header the size of number of columns.
if (this.headerLine == null) {
this.headerLine = generateHeaderLine();
}
}
示例3: main
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
String pathToFile;
if (args.length >= 1) {
pathToFile = args[0];
} else {
pathToFile = "emails.csv";
}
System.out.println("Reading emails from: " + pathToFile);
CSVReader reader = new CSVReader(new FileReader(pathToFile));
List<String> emails = reader.readAll().stream().map(line -> line[0].trim()).collect(Collectors.toList());
System.out.println("Emails count: " + emails.size());
String seedString = "";
List<String> winners = new LotteryMachine(emails, MAX_WINNERS_COUNT).setSeed(seedString).draw();
System.out.println("Winners:");
winners.forEach(System.out::println);
}
示例4: TableDataIterable
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
/**
* Constructor
* @param table_file
* @param has_header whether we expect the data file to include a header in the first row
* @param auto_generate_first_column TODO
* @throws Exception
*/
public TableDataIterable(Table catalog_tbl, File table_file, boolean has_header, boolean auto_generate_first_column) throws Exception {
this.catalog_tbl = catalog_tbl;
this.table_file = table_file;
this.auto_generate_first_column = auto_generate_first_column;
this.types = new int[this.catalog_tbl.getColumnCount()];
this.fkeys = new boolean[this.catalog_tbl.getColumnCount()];
this.nullable = new boolean[this.catalog_tbl.getColumnCount()];
for (int i = 0; i < this.types.length; i++) {
Column catalog_col = this.catalog_tbl.getColumn(i);
this.types[i] = catalog_col.getType();
this.fkeys[i] = (catalog_col.getForeignKey() != null);
this.nullable[i] = catalog_col.isNullable();
} // FOR
this.reader = new CSVReader(FileUtil.getReader(this.table_file));
// Throw away the first row if there is a header
if (has_header) {
this.reader.readNext();
this.line_ctr++;
}
}
示例5: readAllLines
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
/**
* 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 {
System.err.println(String.format("Skipping malformed line: %s",
StringUtils.join(values,",")));
}
}
}
示例6: loadFacesData
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public LinkedHashMap<String, Vector> loadFacesData(String filepath) {
LinkedHashMap<String, Vector> data = new LinkedHashMap<>();
try (CSVReader reader = new CSVReader(new FileReader(filepath), ',')) {
List<String[]> rows = reader.readAll();
double[][] faces = new double[rows.size()][128];
for (int i = 0; i < rows.size(); i++) {
for (int j = 0; j < 128; j++) {
faces[i][j] = Double.parseDouble(rows.get(i)[j]);
}
// we need to convert the format Adrien_Brody_0007.jpg to Adrien_Brody_7
data.put(rows.get(i)[128].replace(".jpg", "").replaceAll("0+(?!$)", ""), new Vector(faces[i]));
}
} catch (IOException ex) {
ex.printStackTrace();
}
return data;
}
示例7: loadCSV
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
private void loadCSV(File fpIn, Map<Integer,Double> mapForB, Map<Integer,Double> mapForD,
Map<Integer,Double> mapForR ) throws IOException {
assertTrue(fpIn.exists());
CSVReader reader = new CSVReader(new FileReader(fpIn));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
String order = nextLine[1].trim();
switch (order) {
case PLACEHOLDER_B : mapForB.put(Integer.parseInt(nextLine[0]), Double.parseDouble(nextLine[2]));
break;
case PLACEHOLDER_D : mapForD.put(Integer.parseInt(nextLine[0]), Double.parseDouble(nextLine[2]));
break;
case PLACEHOLDER_R : mapForR.put(Integer.parseInt(nextLine[0]), Double.parseDouble(nextLine[2]));
break;
}
}
assertTrue(mapForB.size() > 0);
assertEquals(mapForB.size(), mapForD.size());
assertEquals(mapForD.size(), mapForR.size());
reader.close();
}
示例8: testWrite
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
@Test
public void testWrite() throws IOException {
String[] row1 = new String[] { "oneone", "onetwo", "onethree" };
String[] row2 = new String[] { "twoone", "twotwo", "twothree" };
StringWriter sWriter = new StringWriter();
CSVWriter writer = new CSVWriter(sWriter, ',', '\'');
writer.writeNext(row1);
writer.writeNext(row2);
String written = sWriter.toString();
CSVReader reader = new CSVReader(new StringReader(written), ',', '\'');
List<String[]> rows = reader.readAll();
assertTrue(Arrays.equals(rows.get(0), row1));
assertTrue(Arrays.equals(rows.get(1), row2));
}
示例9: addPartialTimesFromFile
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public void addPartialTimesFromFile(Path inputFilePath, FileSystem fileSystem) throws IOException {
// Creates the reader.
FSDataInputStream fileInputStream = fileSystem.open(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
CSVReader fileReader = new CSVReader(bufferedReader);
// Iterates among the lines.
List<String[]> lines = fileReader.readAll();
for (String[] line : lines) {
// Reads data.
int islandNumber = Integer.parseInt(line[ISLAND_NUMBER_PARTIAL_TIMES_HEADER_INDEX]);
long generationsBlockNumber = Long.parseLong(line[GENERATIONS_BLOCK_NUMBER_PARTIAL_TIMES_HEADER_INDEX]);
PhaseType phaseType = PhaseType.valueOf(line[PHASE_TYPE_PARTIAL_TIMES_HEADER_INDEX]);
PartialTimeKey.Type partialTimeType = PartialTimeKey.Type.valueOf(line[TYPE_PARTIAL_TIMES_HEADER_INDEX]);
long time = Long.parseLong(line[TIME_PARTIAL_TIMES_HEADER_INDEX]);
// Adds the time.
this.addPartialTime(islandNumber, generationsBlockNumber, phaseType, partialTimeType, time);
}
}
示例10: main
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
}
// Try writing it back out as CSV to the console
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List allElements = reader2.readAll();
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(allElements);
System.out.println("\n\nGenerated CSV File:\n\n");
System.out.println(sw.toString());
}
示例11: reset
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
@Override
public void reset() throws IOException {
csvReader = new CSVReader(new FileReader(file));
String[] line;
while (null != (line = csvReader.readNext())) {
if (line.length > 0 && ! Check.isEmpty(line[0])) {
headings = line;
break;
}
}
if (headings == null) {
throw new IOException("File only has blank lines");
}
}
示例12: extractRandomConfigs
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public static void extractRandomConfigs(String inputFile, String outputFile, int nConfigs, int nIterations) throws IOException{
CSVReader lines = new CSVReader(new FileReader(inputFile), ',');
List<String[]> content = lines.readAll();
for (int i=0; i<nIterations;i++){
System.out.println(content.isEmpty());
// Shuffle list
Collections.shuffle(content);
int randomNum = ThreadLocalRandom.current().nextInt(1, content.size() - nConfigs);
// Write lines
CSVWriter writer = new CSVWriter(new FileWriter(outputFile+(i+1)+".csv", true),';');
for(int j=randomNum;j<=randomNum+nConfigs-1;j++){
writer.writeNext(content.get(j));
}
writer.close();
}
lines.close();
}
示例13: fromStream
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
/**
* 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);
}
示例14: readMetadataFields
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
public String[] readMetadataFields(CSVReader reader) throws IOException {
// read first line from the file, with metadata description
String[] metadataFields = reader.readNext();
if(metadataFields==null)
throw new RuntimeException("Empty input file");
// if there is a mapping for parameters, use it
boolean hasFile = false;
for(int i=0; i < metadataFields.length; i++) {
String value = getParameter(metadataFields[i]);
if(value != null)
metadataFields[i]=value;
logger.debug("Metadata field " + i + ": " + metadataFields[i]);
if(Parameters.PARAM_FILE.equals(metadataFields[i]))
hasFile=true;
}
if(!hasFile) {
throw new RuntimeException("The header should contain the 'file' parameter or there should be a mapping for one of headers parameters to 'file'");
}
return metadataFields;
}
示例15: testProcessLineWrongNumberOfFields
import au.com.bytecode.opencsv.CSVReader; //导入依赖的package包/类
@Test
public final void testProcessLineWrongNumberOfFields() throws Exception {
setUp();
CSVReader reader = metadataLoader.createReader();
String[] metadataFields = metadataLoader.readMetadataFields(reader);
String[] line = {"a"};
try {
metadataLoader.processLine(line, metadataFields);
} catch (Throwable e) {
assert(true);
return;
}
assert(false);
reader.close();
}