/** * Parses the specified content. * * <p> * See also the various static parse methods on {@link CSVParser}. * </p> * * @param in * the input stream * @return a parser over a stream of {@link CSVRecord}s. * @throws IOException * If an I/O error occurs */ public CSVParser parse(final Reader in) throws IOException { return new CSVParser(in, this); }
public boolean hasNext() { if (CSVParser.this.isClosed()) { return false; } if (this.current == null) { this.current = this.getNextRecord(); } return this.current != null; }
private CSVRecord getNextRecord() { try { return CSVParser.this.nextRecord(); } catch (final IOException e) { // TODO: This is not great, throw an ISE instead? throw new RuntimeException(e); } }
switch (this.reusableToken.type) { case TOKEN: this.addRecordValue(); break; case EORECORD: this.addRecordValue(); break; case EOF: if (this.reusableToken.isReady) { this.addRecordValue(); throw new IOException("(line " + this.getCurrentLineNumber() + ") invalid parse sequence"); case COMMENT: // Ignored currently if(readComment){
protected static List<String[]> readList(Reader reader,CSVFormat format) throws NestedIOException { Args.notNull(reader,"reader"); CSVParser parser = null; try{ parser = new CSVParser(reader, format); return parser.getRecords1(); }catch(IOException e){ throw Exceptions.wrap(e); }finally{ IO.close(reader); IO.close(parser); } }
public static void read(Reader reader,CsvProcessor processor) throws NestedIOException { CSVParser parser = null; try{ parser = new CSVParser(reader, DEFAULT_FORMAT); String[] row; int rownum = 0; while((row = parser.nextRecord1()) != null){ rownum++; processor.process(rownum,row); } }catch(IOException e){ throw Exceptions.wrap(e); }catch(Exception e){ throw Exceptions.uncheck(e); }finally{ IO.close(reader); IO.close(parser); } }
String[] nextRecord1() throws IOException { if(tryNextRecord()){ return this.record.toArray(new String[this.record.size()]); } return null; }
List<String[]> getRecords1() throws IOException { final List<String[]> records = new ArrayList<String[]>(); String[] rec; while ((rec = this.nextRecord1()) != null) { records.add(rec); } return records; }
/** * Customized CSV parser using the given {@link CSVFormat} * * <p> * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser, * unless you close the {@code reader}. * </p> * * @param reader * a Reader containing CSV-formatted input. Must not be null. * @param format * the CSVFormat used for CSV parsing. Must not be null. * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either reader or format are null. * @throws IOException * If an I/O error occurs */ public CSVParser(final Reader reader, final CSVFormat format) throws IOException { Args.notNull(reader, "reader"); Args.notNull(format, "format"); format.validate(); this.format = format; this.lexer = new Lexer(format, new ExtendedBufferedReader(reader)); this.headerMap = this.initializeHeader(); }
/** * Parses the next record from the current point in the stream. * * @return the record as an array of values, or <tt>null</tt> if the end of the stream has been reached * @throws IOException * on parse error or input read-failure */ CSVRecord nextRecord() throws IOException { if(tryNextRecord()){ return new CSVRecord(this.record.toArray(new String[this.record.size()]), this.headerMap,recordComment,this.recordNumber); } return null; }
/** * Creates a parser for the given {@link String}. * * @param string * a CSV string. Must not be null. * @param format * the CSVFormat used for CSV parsing. Must not be null. * @return a new parser * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either string or format are null. * @throws IOException * If an I/O error occurs */ public static CSVParser parse(String string, final CSVFormat format) throws IOException { Args.notNull(string, "string"); Args.notNull(format, "format"); return new CSVParser(new StringReader(string), format); }
/** * Parses the CSV input according to the given format and returns the content as an array of {@link CSVRecord} * entries. * <p/> * The returned content starts at the current parse-position in the stream. * * @return list of {@link CSVRecord} entries, may be empty * @throws IOException * on parse error or input read-failure */ public List<CSVRecord> getRecords() throws IOException { final List<CSVRecord> records = new ArrayList<CSVRecord>(); CSVRecord rec; while ((rec = this.nextRecord()) != null) { records.add(rec); } return records; }
public CSVRecord next() { if (CSVParser.this.isClosed()) { throw new NoSuchElementException("CSVParser has been closed"); } CSVRecord next = this.current; this.current = null; if (next == null) { // hasNext() wasn't called before next = this.getNextRecord(); if (next == null) { throw new NoSuchElementException("No more CSV records available"); } } return next; }
/** * Creates a parser for the given {@link File}. * * @param file * a CSV file. Must not be null. * @param format * the CSVFormat used for CSV parsing. Must not be null. * @return a new parser * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either file or format are null. * @throws IOException * If an I/O error occurs */ public static CSVParser parse(File file, final CSVFormat format) throws IOException { Args.notNull(file, "file"); Args.notNull(format, "format"); return new CSVParser(new FileReader(file), format); }
/** * Initializes the name to index mapping if the format defines a header. */ private Map<String, Integer> initializeHeader() throws IOException { Map<String, Integer> hdrMap = null; final String[] formatHeader = this.format.getHeader(); if (formatHeader != null) { hdrMap = new LinkedHashMap<String, Integer>(); String[] header = null; if (formatHeader.length == 0) { // read the header from the first line of the file final CSVRecord record = this.nextRecord(); if (record != null) { header = record.values(); } } else { if (this.format.getSkipHeaderRecord()) { this.nextRecord(); } header = formatHeader; } // build the name to index mappings if (header != null) { for (int i = 0; i < header.length; i++) { hdrMap.put(header[i], Integer.valueOf(i)); } } } return hdrMap; }
/** * Creates a parser for the given URL. * * <p> * If you do not read all records from the given {@code url}, you should call {@link #close()} on the parser, unless * you close the {@code url}. * </p> * * @param url * a URL. Must not be null. * @param charset * the charset for the resource. Must not be null. * @param format * the CSVFormat used for CSV parsing. Must not be null. * @return a new parser * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either url, charset or format are null. * @throws IOException * If an I/O error occurs */ public static CSVParser parse(URL url, Charset charset, final CSVFormat format) throws IOException { Args.notNull(url, "url"); Args.notNull(charset, "charset"); Args.notNull(format, "format"); return new CSVParser(new InputStreamReader(url.openStream(), charset == null ? Charset.forName("UTF-8") : charset), format); }