/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
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); } }