本文整理匯總了Java中org.apache.commons.csv.CSVParser.getRecords方法的典型用法代碼示例。如果您正苦於以下問題:Java CSVParser.getRecords方法的具體用法?Java CSVParser.getRecords怎麽用?Java CSVParser.getRecords使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.csv.CSVParser
的用法示例。
在下文中一共展示了CSVParser.getRecords方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parse
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public List<PhotoLocationSightPair> parse () throws IOException, BuenOjoCSVParserException {
CSVParser parser = CSVFormat.RFC4180.withHeader().withDelimiter(',').withAllowMissingColumnNames(true).parse(new InputStreamReader(this.inputStreamSource.getInputStream()));
List<CSVRecord> records = parser.getRecords();
if (records.size() == 0 ) {
throw new BuenOjoCSVParserException("El archivos de miras no contiene registros");
}
ArrayList<PhotoLocationSightPair> sightPairs = new ArrayList<>(records.size());
for (CSVRecord record : records) {
PhotoLocationSightPair sight = new PhotoLocationSightPair();
sight.setNumber(new Integer(record.get(PhotoLocationSightPairCSVColumn.id)));
sight.setSatelliteX(new Integer(record.get(PhotoLocationSightPairCSVColumn.satCol)));
sight.setSatelliteY(new Integer(record.get(PhotoLocationSightPairCSVColumn.satRow)));
sight.setSatelliteTolerance(new Integer(record.get(PhotoLocationSightPairCSVColumn.satTolerancia)));
sight.setTerrainX(new Integer(record.get(PhotoLocationSightPairCSVColumn.terCol)));
sight.setTerrainY(new Integer(record.get(PhotoLocationSightPairCSVColumn.terRow)));
sight.setTerrainTolerance(new Integer(record.get(PhotoLocationSightPairCSVColumn.terTolerancia)));
sightPairs.add(sight);
}
return sightPairs;
}
示例2: parse
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public PhotoLocationBeacon parse() throws IOException, BuenOjoCSVParserException {
CSVParser parser = CSVFormat.RFC4180.withHeader().withDelimiter(',').withAllowMissingColumnNames(true).parse(new InputStreamReader(this.inputStreamSource.getInputStream()));
List<CSVRecord> records = parser.getRecords();
if (records.size() > 1) {
throw new BuenOjoCSVParserException("El archivo contiene más de un indicador");
}
if (records.size() == 0) {
throw new BuenOjoCSVParserException("El archivo de indicador es inválido");
}
CSVRecord record = records.get(0);
PhotoLocationBeacon beacon = new PhotoLocationBeacon();
beacon.setX(new Integer(record.get(PhotoLocationBeaconCSVColumns.col.ordinal())));
beacon.setY(new Integer(record.get(PhotoLocationBeaconCSVColumns.row.ordinal())));
beacon.setTolerance(new Integer(record.get(PhotoLocationBeaconCSVColumns.tolerance.ordinal())));
return beacon;
}
示例3: setupUtils
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
protected void setupUtils() throws Exception {
CSVFormat format = CSVFormat.DEFAULT;
String fileLocation = config.getFileLocation();
URL url;
try {
url = new URL(fileLocation);
} catch (MalformedURLException e) {
File file;
if (!(file = new File(fileLocation)).exists()) {
log.error("File does not exist: ", fileLocation);
}
url = file.toURI().toURL();
}
InputStreamReader isr = new InputStreamReader(
downloadUtils.fetchInputStream(url, getProvider().getLabel(), ".csv"));
CSVParser csvFileParser = new CSVParser(isr, format);
csvRecords = csvFileParser.getRecords();
}
示例4: testReaderFromURL
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
@SuppressWarnings("resource")
// @Test
public void testReaderFromURL() throws UIMAException, IOException {
CSVParser reader = new CSVParser(new FileReader(new File(csvFilename)),
CSVFormat.TDF.withHeader((String) null));
List<CSVRecord> records = reader.getRecords();
description = CollectionReaderFactory.createReaderDescription(TextgridTEIUrlReader.class,
TextgridTEIUrlReader.PARAM_INPUT, csvFilename, TextgridTEIUrlReader.PARAM_LANGUAGE, "de");
JCasIterator iter = SimplePipeline
.iteratePipeline(description, AnalysisEngineFactory.createEngineDescription(XmiWriter.class,
XmiWriter.PARAM_TARGET_LOCATION, "target/doc/", XmiWriter.PARAM_USE_DOCUMENT_ID, true))
.iterator();
JCas jcas;
CSVRecord gold;
int recordIndex = 0;
while (iter.hasNext()) {
jcas = iter.next();
gold = records.get(recordIndex++);
checkSanity(jcas);
checkGold(jcas, gold);
}
}
示例5: getResponse
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Retrieves the answers of responseID for the survey identified by surveyID
*
* @param surveyID
* identifier of the survey
* @param responseID
* identifier of the response
* @return the answers of responseID for the survey identified by surveyID
* @throws ClientProtocolException
* @throws IOException
*/
public Hashtable<String, String> getResponse(int surveyID, int responseID)
throws ClientProtocolException, IOException {
String sessionKey = getSessionKey();
String responses = exportResponseByResponseID(sessionKey, surveyID,
responseID);
CSVParser csvParser = CSVParser.parse(responses, CSVFormat.RFC4180);
List<CSVRecord> csvRecordsList = csvParser.getRecords();
CSVRecord csvQuestions = csvRecordsList.get(0);
CSVRecord csvAnswers = csvRecordsList.get(1);
Iterator<String> iterQuestion = csvQuestions.iterator();
Iterator<String> iterAnswer = csvAnswers.iterator();
Hashtable<String, String> questionAnswerDictionary = new Hashtable<String, String>();
while (iterQuestion.hasNext()) {
String question = iterQuestion.next();
String answer = iterAnswer.next();
if (!isIgnoredKey(question))
questionAnswerDictionary.put(formatQuestion(question),
formatAnswer(answer));
}
return questionAnswerDictionary;
}
示例6: loadTableFromCSV
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static Table loadTableFromCSV(String projectName, String tableName,
String csvFilePath, Attribute idAttrib, List<Attribute> attributes)
throws IOException{
FileReader r = new FileReader(csvFilePath);
CSVParser parser = new CSVParser(r,CSVFormat.DEFAULT);
List<CSVRecord> records = parser.getRecords();
r.close();
List<Tuple> tuples = new ArrayList<Tuple>();
int size = records.size();
// System.out.println("No. of tuples: " + size);
for(int i = 0; i < size; i++){
CSVRecord rec = records.get(i);
Map<Attribute,Object> attrValMap = new HashMap<Attribute,Object>();
for(int j = 0; j < attributes.size(); j++){
Attribute a = attributes.get(j);
String value = rec.get(j);
attrValMap.put(a, a.convertValueToObject(value));
//System.out.println("i: " + i + ", j: "+ j);
}
tuples.add(new Tuple(attrValMap));
}
// System.out.println("Size of tuples: " + tuples.size());
return new Table(tableName, idAttrib, attributes, tuples, projectName);
}
示例7: read
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static Table read(String projectName, String tableName,
String csvFilePath, Attribute idAttrib, List<Attribute> attributes)
throws IOException {
FileReader r = new FileReader(csvFilePath);
CSVParser parser = new CSVParser(r);
List<CSVRecord> records = parser.getRecords();
r.close();
List<Item> items = new ArrayList<Item>();
for(int i=1; i< records.size(); i++){
CSVRecord rec = records.get(i);
Map<Attribute,Object> attrValMap = new HashMap<Attribute,Object>();
for(int j=0; j<attributes.size(); j++){
Attribute a = attributes.get(j);
String value = rec.get(j);
attrValMap.put(a, value);
}
items.add(new Item(attrValMap));
}
return new Table(tableName,idAttrib,attributes,items,projectName);
}
示例8: loadFunctions
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static List<Function> loadFunctions(String csvFunctionFilePath) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
List<Function> functions = new ArrayList<Function>();
try {
FileReader r = new FileReader(csvFunctionFilePath);
CSVParser parser = new CSVParser(r);
List<CSVRecord> records = parser.getRecords();
r.close();
int size = records.size();
for(int i = 1; i < size; i++){
CSVRecord rec = records.get(i);
String functionName = rec.get(0).trim();
String functionDescription = rec.get(1).trim();
String className = rec.get(2).trim();
Class<?> functionClass = Class.forName(className);
Constructor<?> constructor = functionClass.getConstructor(String.class, String.class);
Function function = (Function) constructor.newInstance(functionName, functionDescription);
functions.add(function);
}
}
catch(FileNotFoundException fnfe) {
System.out.println("File not found: " + fnfe.getMessage());
}
// System.out.println("No. of functions: " + functions.size());
return functions;
}
示例9: readRules
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static List<Rule> readRules(Project project, String table1Name,
String table2Name, String csvRuleFilePath) throws IOException{
List<Rule> rules = new ArrayList<Rule>();
try {
FileReader r = new FileReader(csvRuleFilePath);
CSVParser parser = new CSVParser(r);
List<CSVRecord> records = parser.getRecords();
r.close();
int size = records.size();
for(int i = 1; i < size; i++){
CSVRecord rec = records.get(i);
String ruleName = rec.get(0).trim();
String ruleString = rec.get(1).trim();
List<Term> terms = ParsingUtils.parseRule(project, ruleString);
Rule rule = new Rule(ruleName, project.getName(), table1Name,
table2Name, terms);
rules.add(rule);
}
}
catch(FileNotFoundException fnfe) {
System.out.println("File not found: " + fnfe.getMessage());
}
// System.out.println("No. of rules: " + rules.size());
return rules;
}
示例10: csv2meld
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static void csv2meld(CSVParser reader, Function<String, String> headerExtractor, Function<String, Object> valueExtractor, MeldWriter writer) throws IOException, ReconException {
ReconTable table = writer.addTable("csv", Iterables.toArray(Iterables.transform(reader.getHeaderMap().keySet(), headerExtractor), String.class));
writer.finalizeDefinitions();
writer.flush();
List<CSVRecord> rows = reader.getRecords();
for (String column : reader.getHeaderMap().keySet()) {
List<Object> values = new ArrayList<Object>(rows.size());
for (CSVRecord row : rows) {
String entry = row.get(column);
values.add(valueExtractor.apply(entry));
}
table.setSignal(headerExtractor.apply(column), values.toArray());
writer.flush();
}
writer.close();
}
示例11: parseAndInject
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public void parseAndInject(PhotoLocationExercise exercise, InputStreamSource inputStreamSource) throws BuenOjoCSVParserException, IOException{
CSVParser parser = CSVFormat.RFC4180.withHeader().withDelimiter(',').withAllowMissingColumnNames(true).parse(new InputStreamReader(inputStreamSource.getInputStream()));
List<CSVRecord> records = parser.getRecords();
if (records.size() > 1) {
throw new BuenOjoCSVParserException("El archivo contiene más de un ejercicio");
}
if (records.size() == 0) {
throw new BuenOjoCSVParserException("El archivo de ejericio es inválido");
}
CSVRecord record = records.get(0);
String name = record.get(MetadataColumns.name);
String description = record.get(MetadataColumns.description);
String difficulty = record.get(MetadataColumns.difficulty);
String seconds = record.get(MetadataColumns.seconds);
String totalScore = record.get(MetadataColumns.totalScore.ordinal());
String imageName = record.get(MetadataColumns.imageName.ordinal());
exercise.setDescription(description);
exercise.setName(name);
exercise.setDifficulty(difficultyFromString(difficulty));
exercise.setTotalTimeInSeconds(new Integer(seconds));
exercise.setTotalScore(new Float(totalScore));
exercise.setExtraPhotosCount(3);
List<PhotoLocationImage> imgs = photoLocationImageRepository.findAll();
Optional<PhotoLocationImage> opt = imgs.stream().filter(p -> p.getImage().getName().equals(imageName)).collect(Collectors.toList()).stream().findFirst();
if (!opt.isPresent()){
throw new BuenOjoCSVParserException("la imagen '"+imageName+"' no existe en la base de datos");
}
PhotoLocationImage img = opt.get();
img.setKeywords(exercise.getLandscapeKeywords());
photoLocationImageRepository.save(img);
exercise.setTerrainPhoto(img);
}
示例12: getRecords
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
private List<CSVRecord> getRecords () throws BuenOjoCSVParserException, IOException{
CSVParser parser = CSVFormat.RFC4180.withHeader().withDelimiter(',').withAllowMissingColumnNames(true).parse(new InputStreamReader(this.inputStreamSource.getInputStream()));
List<CSVRecord> records = parser.getRecords();
if (records.size()==0) {
throw new BuenOjoCSVParserException("El archivo no contiene palabras clave");
}
if (records.size()>1) {
throw new BuenOjoCSVParserException("El archivo contiene mas de un registro de palabras clave");
}
return records;
}
示例13: loadFileinTable
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static void loadFileinTable(File file, JTable table) {
if (file.exists()) {
try (Reader in = new FileReader(file)) {
CSVParser parser = CSVFormat.EXCEL.withHeader().withSkipHeaderRecord().withIgnoreEmptyLines().parse(in);
if (!parser.getHeaderMap().isEmpty()) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (String columnHeader : parser.getHeaderMap().keySet()) {
if (!columnHeader.trim().isEmpty()) {
model.addColumn(columnHeader);
}
}
List<CSVRecord> records = parser.getRecords();
for (CSVRecord record : records) {
Object[] row = new Object[record.size()];
for (int i = 0; i < record.size(); i++) {
row[i] = record.get(i);
}
model.addRow(row);
}
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
} else {
LOGGER.log(Level.SEVERE, "File [{0}] doesn''t exist", file.getAbsolutePath());
}
}
示例14: getRecords
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static List<CSVRecord> getRecords(File file) {
if (file.exists()) {
try (Reader in = new FileReader(file)) {
CSVParser parser = CSVFormat.EXCEL.withHeader().withSkipHeaderRecord().withIgnoreEmptyLines().parse(in);
if (!parser.getHeaderMap().isEmpty()) {
return parser.getRecords();
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
} else {
LOGGER.log(Level.SEVERE, "File [{0}] doesn''t exist", file.getAbsolutePath());
}
return new ArrayList<>();
}
示例15: getCSVHParser
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static CSVHParser getCSVHParser(File file) {
if (file.exists()) {
try (Reader in = new FileReader(file)) {
CSVParser parser = CSVFormat.EXCEL.withHeader().withSkipHeaderRecord().withIgnoreEmptyLines().parse(in);
if (!parser.getHeaderMap().isEmpty()) {
return new CSVHParser(parser.getHeaderMap(), parser.getRecords());
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
} else {
LOGGER.log(Level.SEVERE, "File [{0}] doesn''t exist", file.getAbsolutePath());
}
return null;
}