public final class CsvMethods extends Object
This class provides one-liners for CSV related operations.
A CSV is a comma separated values file which allows data to be saved in a table structured format. Used when parsing CSV files, to easily to go through a bulk of data in a CSV file.
Modifier and Type | Method and Description |
---|---|
static <T> T |
eachRecord(File self,
char delimiter,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(File self,
Closure<T> closure)
Parse CSV file.
|
static <T> T |
eachRecord(File self,
CSVFormat format,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(File self,
int firstLine,
char delimiter,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(File self,
int firstLine,
Closure<T> closure)
Parse CSV file.
|
static <T> T |
eachRecord(File self,
int firstLine,
CSVFormat format,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(File self,
int firstLine,
int sheet,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(File self,
int firstLine,
List indexes,
Closure<T> closure)
Parse a fixed-width file.
|
static <T> T |
eachRecord(InputStream self,
int firstLine,
List indexes,
Closure<T> closure)
Parse a fixed-width file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
char delimiter,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
CSVFormat format,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
int firstLine,
char delimiter,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
int firstLine,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
int firstLine,
CSVFormat format,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(InputStream self,
String mimeType,
int firstLine,
int sheet,
Closure<T> closure)
Parse a flat file.
|
static <T> T |
eachRecord(Reader self,
int firstLine,
List indexes,
Closure<T> closure)
Parse a fixed-width file.
|
public static <T> T eachRecord(File self, Closure<T> closure) throws IOException, ToroException
Parse CSV file.
Example usage:
'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord(){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parseclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, int firstLine, Closure<T> closure) throws IOException, ToroException
Parse CSV file.
Example usage:
def startIndex = 10 'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( startIndex ) { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parsefirstLine
- Start index of record to be parseclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( ',' ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parsedelimiter
- Character used to split the dataclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, int firstLine, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def startIndex = 10 'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( startIndex , ',' ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parsefirstLine
- Start index of record to be parsedelimiter
- Character used to split the dataclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( CSVFormat.RFC4180 ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parseformat
- Format of the CSV fileclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, int firstLine, CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def startIndex = 10 'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( startIndex , CSVFormat.RFC4180 ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parsefirstLine
- Start index of record to be parseformat
- Format of the CSV fileclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, int firstLine, int sheet, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def startIndex = 10 def startSheetIndex = 2 'ftp://username:password@host:21/path/to/csv/file.csv'.vfsGet().eachRecord( startIndex , startSheetIndex ) { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
self
- CSV file to be parsefirstLine
- Start index of record to be parsesheet
- The sheet index in a spreadsheetclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".vfsGet().file().getInputStream() is.eachRecord() { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream dataclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".vfsGet().file().getInputStream() def startIndex = 10 is.eachRecord( startIndex ) { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream datafirstLine
- Start index of record to be parseclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".vfsGet().file().getInputStream() is.eachRecord( ',' ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream datadelimiter
- Character used to split the dataclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".vfsGet().file().getInputStream() def startIndex = 10 is.eachRecord( startIndex , ',' ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream datafirstLine
- Start index of record to be parsedelimiter
- Character used to split the dataclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".file().getInputStream() is.eachRecord( CSVFormat.RFC4180 ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream dataformat
- Format of the CSV fileclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".file().getInputStream() def startIndex = 10 is.eachRecord( startIndex , CSVFormat.RFC4180 ) { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream datafirstLine
- Start index of record to be parseformat
- Format of the CSV fileclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, int sheet, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be CSV, tab delimited file, or an Excel spreadsheet.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.csv".file().getInputStream() def startIndex = 10 def startSheetIndex = 2 is.eachRecord( startIndex , startSheetIndex ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsemimeType
- MIME type of the InputStream datafirstLine
- Start index of record to be parsesheet
- The sheet index in a spreadsheetclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(File self, int firstLine, List indexes, Closure<T> closure) throws IOException, ToroException
Parse a fixed-width file.
Example usage:
File f =new File('/tmp/fixed-width-data.txt') f.eachRecord( 10, [ 32, 134, 176, 217, 255 ] ) { def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- Fixed-width file to be parsedfirstLine
- Start index of record to be parsedindexes
- The list indexesclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(InputStream self, int firstLine, List indexes, Closure<T> closure) throws IOException, ToroException
Parse a fixed-width file.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.txt".file().getInputStream() is.eachRecord( 10, [ 32, 134, 176, 217, 255 ] ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- Fixed-width file to be parsedfirstLine
- Start index of record to be parsedindexes
- The list indexesclosure
- The closure containing operations and optional statementsIOException
ToroException
public static <T> T eachRecord(Reader self, int firstLine, List indexes, Closure<T> closure) throws IOException, ToroException
Parse a fixed-width file.
Example usage:
def is = "ftp://username:password@host:21/path/to/csv/file.txt".file().getInputStream() def reader = new InputStreamReader( is ) reader.eachRecord( 10, [ 32, 134, 176, 217, 255 ] ){ def data = [it[0], it[1], it[2]] println "Processed data: " + data }
The Input Stream will be closed for you
self
- CSV file to be parsefirstLine
- Start index of record to be parseindexes
- The list indexesclosure
- The closure containing operations and optional statementsIOException
ToroException
Copyright © 2019. All rights reserved.