本文整理匯總了Java中org.apache.commons.csv.CSVParser.close方法的典型用法代碼示例。如果您正苦於以下問題:Java CSVParser.close方法的具體用法?Java CSVParser.close怎麽用?Java CSVParser.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.csv.CSVParser
的用法示例。
在下文中一共展示了CSVParser.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
CSVParser parser = CSVParser.parse(new File("dev/twitter-hate-speech-processed.csv"), Charset.forName("Cp1252"), CSVFormat.DEFAULT);
try (PrintWriter writer = new PrintWriter("training/bad/model_comments_bad_tweets.txt", "UTF-8")) {
boolean skipFirst = true;
for (CSVRecord r : parser) {
if (skipFirst){
skipFirst=false;
continue;
}
String classif = r.get(0);
if (classif.equalsIgnoreCase("The tweet is not offensive")) {
continue;
}
writer.println(r.get(2));
}
}finally {
parser.close();
}
}
示例2: splitStr
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* String Parsing
*/
public static String[] splitStr(String val, Integer len) throws IOException {
String[] input;
try {
CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT);
CSVRecord record = parser.getRecords().get(0);
input = new String[len];
Iterator<String> valuesIt = record.iterator();
int i = 0;
while (valuesIt.hasNext()) {
input[i] = valuesIt.next().trim();
i++;
}
parser.close();
} catch (ArrayIndexOutOfBoundsException e) {
input = val.split(",", len);
for (int i = 0; i < input.length; i++)
input[i] = input[i].trim();
}
return input;
}
示例3: addLabels
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Adds labels read in a CSV file to the Jena model.
*
* @param filePath The path of the CSV file.
* @param version The version of the CPC classification.
* @param language The tag representing the language of the labels ("fr", "es", etc.).
*/
private void addLabels(String filePath, String version, String language) {
if (filePath == null) return;
logger.debug("Preparing to create additional labels for version " + version + ", language is " + language);
try {
Reader reader = new InputStreamReader(new FileInputStream(filePath), "Cp1252");
CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());
for (CSVRecord record : parser) {
String itemCode = record.get(0);
Resource itemResource = cpcModel.createResource(Names.getItemURI(itemCode, "CPC", version));
itemResource.addProperty(SKOS.prefLabel, cpcModel.createLiteral(record.get(1), language));
}
parser.close();
reader.close();
} catch (Exception e) {
logger.error("Error adding labels from " + filePath, e);
}
}
示例4: addLabels
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Adds labels read in a CSV file to the Jena model.
*
* @param filePath The path of the CSV file.
* @param version The version of the ISIC classification.
* @param language The tag representing the language of the labels ("fr", "es", etc.).
*/
private void addLabels(String filePath, String version, String language) {
if (filePath == null) return;
logger.debug("Preparing to create additional labels, language is " + language + ", source file is " + filePath);
try {
Reader reader = new InputStreamReader(new FileInputStream(filePath), "Cp1252");
CSVParser parser = new CSVParser(reader, ISIC_LABELS_FILE_FORMAT.get(version));
for (CSVRecord record : parser) {
String itemCode = record.get(0);
Resource itemResource = isicModel.createResource(Names.getItemURI(itemCode, "ISIC", version));
itemResource.addProperty(SKOS.prefLabel, isicModel.createLiteral(record.get(1), language));
}
parser.close();
reader.close();
} catch (Exception e) {
logger.error("Error adding labels from " + filePath, e);
}
}
示例5: parseFile
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
static void parseFile(final String fileStr, final PublishSubject<Map<String, String>> subject) throws Exception {
final CSVParser parser = CSVParser.parse(
new File(fileStr), StandardCharsets.UTF_8, CSVFormat.DEFAULT
);
try {
parser.forEach(csvRecord -> subject.onNext(toMap(csvRecord)));
subject.onComplete();
} finally {
parser.close();
}
}
示例6: importData
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, InterruptedException
{
final CSVParser parser = new CSVParser(input, CSVFormat.RFC4180.withHeader());
SQLiteDatabase database = db.getWritableDatabase();
database.beginTransaction();
try
{
for (CSVRecord record : parser)
{
importLoyaltyCard(database, db, record);
if(Thread.currentThread().isInterrupted())
{
throw new InterruptedException();
}
}
parser.close();
database.setTransactionSuccessful();
}
catch(IllegalArgumentException|IllegalStateException e)
{
throw new FormatException("Issue parsing CSV data", e);
}
finally
{
database.endTransaction();
database.close();
}
}
示例7: readCSV
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* test readCSV() function
* @return
*/
public String readCSV(){
StringBuffer sb = new StringBuffer();
try{
filePath = servletContext.getRealPath("WEB-INF/csv/pending.csv");
File f = new File(filePath);
System.out.println(f.exists());
FileReader fr = new FileReader(f);
CSVParser parser = new CSVParser(fr, CSVFormat.DEFAULT);
List<CSVRecord> l = parser.getRecords();
for (CSVRecord r:l){
System.out.println(r);
sb.append(r.get(0));
//sb.append(r.get(1));
sb.append(r.get(2));
sb.append(r.get(3));
sb.append(r.get(4));
sb.append("| ");
}
fr.close();
parser.close();
}catch (Exception e){
e.printStackTrace();
}
return sb.toString();
}
示例8: parseLines
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
private Map<String, Pair<Integer, Integer>> parseLines(String instanceTypeCsv) throws IOException, URISyntaxException, ParseException {
InputStreamReader inputStreamReader;
if (instanceTypeCsv.equalsIgnoreCase("classpath")) {
inputStreamReader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("instancetypes.csv"), Charset.forName("UTF-8"));
} else {
URI uri = new URI(instanceTypeCsv);
inputStreamReader = new InputStreamReader(new FileInputStream(new File(uri)), Charset.forName("UTF-8"));
}
CSVParser parser = null;
List<CSVRecord> lines = null;
try {
parser = CSVFormat.DEFAULT.parse(new BufferedReader(inputStreamReader));
lines = parser.getRecords();
} finally {
inputStreamReader.close();
if (parser != null) {
parser.close();
}
}
if (lines == null || lines.size() <= 0) {
throw new ParseException("instance-type-csv data is empty");
}
Map<String, Pair<Integer, Integer>> linesMap = new HashMap<String, Pair<Integer, Integer>>();
for (CSVRecord current : lines) {
linesMap.put(current.get(1), new ImmutablePair<Integer, Integer>(Integer.parseInt(current.get(2)), Integer.parseInt(current.get(3))));
}
return linesMap;
}
示例9: readFile
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Reads the csv file of the TagDownloader
*/
public static void readFile(File file) {
try {
logger.info("Starting to read file of TagDownloader ...");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new BZip2CompressorInputStream(
new BufferedInputStream(
new FileInputStream(file))), "UTF-8"));
CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180);
dataStore.connect();
for (CSVRecord csvRecord: parser) {
parseRecord(csvRecord);
if (csvRecord.getRecordNumber() % 1000000 == 0) {
logger.info("Current Record: " + csvRecord.getRecordNumber());
}
}
dataStore.disconnect();
parser.close();
logger.info("Tag Distribution:\n"
+ FrequencyUtils.formatFrequency(tagDistribution));
logger.info("Finished");
} catch (Exception e) {
logger.error("", e);
}
}
示例10: readFile
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Reads the csv file of the TagDownloader
*/
public static void readFile(File file) {
try {
logger.info("Starting to read file of GeolocationDatabase ...");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new BZip2CompressorInputStream(
new BufferedInputStream(
new FileInputStream(file))), "UTF-8"));
CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180);
for (CSVRecord csvRecord: parser) {
parseRecord(csvRecord);
if (csvRecord.getRecordNumber() % 1000000 == 0) {
logger.info("Current Record: " + csvRecord.getRecordNumber());
}
}
parser.close();
logger.info("Finished");
} catch (Exception e) {
logger.error("", e);
}
}
示例11: getResourceById
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public DBpediaResource getResourceById(String resourceId) {
Map<String, List<String>> properties = new HashMap<>();
try {
CSVParser parser = execute(PROPERTIES_FOR_ENTITY.replace(":resourceId", resourceId));
for (CSVRecord record : parser) {
List<String> propertyContainer = properties.getOrDefault(record.get("relation"), new LinkedList<>());
propertyContainer.add(record.get("property"));
properties.put(record.get("relation"), propertyContainer);
}
parser.close();
} catch (URISyntaxException | IOException e) {
logger.error(e);
}
return new DBpediaResource(resourceId, properties);
}
示例12: load
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Load the data table file (in CSV format) to memory.
*
* @param in
* the {@link InputStream} contains the content of data table
* file
* @return the data table
* @throws IOException
*/
public DataTable load(InputStream in) throws IOException {
List<List<Object>> rows = new LinkedList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
// don't use csvParser.getRecords() for iteration, may cause huge memory usage and lead to out-of-memory error
for (CSVRecord csvRecord : csvParser)
rows.add(getFields(csvRecord));
csvParser.close();
/*String line;
while ((line = reader.readLine()) != null) {
rows.add(getFields(line));
}*/
return new DataTable(rows);
}
示例13: readFromCsv
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static ReadResult readFromCsv(InputStream input, Consumer<? super CSVRecord> action) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input, CHARSET), READ_BUFFER_SIZE);
CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL
.withDelimiter(';')
.withRecordSeparator('\n')
);
RecordReaderWrapper wrapper = new RecordReaderWrapper(action);
parser.forEach(wrapper);
parser.close();
reader.close();
return new ReadResult(wrapper.readedReacords, wrapper.successfulReadedRecors);
}
示例14: getInputFromCSV
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Takes a CSV File and parses into a List of CSVRecords objects. Also verifies
* that the SYMBOL variable exists in the csv since that is the critical piece
* @param csvFile the file to input
* @return List of CSVRecords
* @throws IOException
*/
private static List<CSVRecord> getInputFromCSV(File csvFile)
throws IOException {
FileReader fr = new FileReader(csvFile);
// For Testing
try {
printFile(csvFile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CSVParser csvp = new CSVParser(fr, CSVFormat.EXCEL.withHeader());
List<CSVRecord> list = csvp.getRecords();
Map<String, Integer> headers = csvp.getHeaderMap();
// Get Header map and index of SYMBOL
try {
// Sole purpose of i is to check to see if Symbol exists
@SuppressWarnings("unused")
int i = headers.get(SYMBOL).intValue();
csvp.close();
return list;
} catch (NullPointerException e) {
System.err.println("No " + SYMBOL + " Column Found");
csvp.close();
throw new IOException("Invalid File Format");
}
}
示例15: getLines
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public Collection<CsvLine> getLines() throws RtException {
Check.notNull(format, "format");
Check.notEmpty(fileContent, "fileContent");
// withSkipHeaderRecord(true) only works with a header definition,
// but our columns are dynamic (no fixed number)
// => hence we need to exclude the header anyway, without relying
// on the commons CSV parser behaviour
boolean skipFirstLine = getFormat().getSkipHeaderRecord();
log.debug("Parsing CSV lines with format {} ", getFormat());
CSVParser parser = null;
List<CSVRecord> records = null;
try {
parser = CSVParser.parse(getFileContent(), getFormat());
records = parser.getRecords();
} catch (IOException ioe) {
log.error("Unable to parse CSV content ", ioe);
throw new RtException(ioe.getMessage(), ioe);
} finally {
if (null != parser) {
try {
parser.close();
} catch (IOException ignore) { }
}
}
return toCsvLines(records, skipFirstLine);
}