Class CsvMethods
- java.lang.Object
-
- io.toro.martini.CsvMethods
-
public final class CsvMethods extends Object
Provides one-liners for CSV-related operations, so you could easily parse and go through data within CSV files. A CSV is a comma separated values file which allows data to be saved in a table structured format.
-
-
Constructor Summary
Constructors Constructor Description CsvMethods(io.toro.martini.parser.flatfile.ESBFlatFileManager esbFlatFileManager)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
eachRecord(File self, char delimiter, 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, int sheet, Closure<T> closure)
Parse a flat file.static <T> T
eachRecord(File self, int firstLine, Closure<T> closure)
Parse a CSV file.static <T> T
eachRecord(File self, int firstLine, List indexes, Closure<T> closure)
Parse a fixed-width file.static <T> T
eachRecord(File self, int firstLine, org.apache.commons.csv.CSVFormat format, Closure<T> closure)
Parse a flat file.static <T> T
eachRecord(File self, Closure<T> closure)
Parse a CSV file.static <T> T
eachRecord(File self, org.apache.commons.csv.CSVFormat format, Closure<T> closure)
Parse a flat 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, int firstLine, char delimiter, 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(InputStream self, String mimeType, int firstLine, Closure<T> closure)
Parse a flat file.static <T> T
eachRecord(InputStream self, String mimeType, int firstLine, org.apache.commons.csv.CSVFormat format, 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, org.apache.commons.csv.CSVFormat format, 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.
-
-
-
Constructor Detail
-
CsvMethods
@Autowired CsvMethods(io.toro.martini.parser.flatfile.ESBFlatFileManager esbFlatFileManager)
-
-
Method Detail
-
eachRecord
public static <T> T eachRecord(File self, Closure<T> closure) throws IOException, ToroException
Parse a 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 }
- Parameters:
self
- CSV file to parseclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, int firstLine, Closure<T> closure) throws IOException, ToroException
Parse a 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 }
- Parameters:
self
- CSV file to parsefirstLine
- the index of the record where parsing should startclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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 }
- Parameters:
self
- CSV file to parsedelimiter
- character used to split the dataclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, int firstLine, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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 }
- Parameters:
self
- CSV file to parsefirstLine
- the index of the record where parsing should startdelimiter
- character used to split the dataclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, org.apache.commons.csv.CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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 }
- Parameters:
self
- CSV file to parseformat
- format of the CSV fileclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, int firstLine, org.apache.commons.csv.CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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 }
- Parameters:
self
- CSV file to parsefirstLine
- the index of the record where parsing should startformat
- format of the CSV fileclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(File self, int firstLine, int sheet, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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 }
- Parameters:
self
- CSV file to parsefirstLine
- the index of the record where parsing should startsheet
- the sheet index in a spreadsheetclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(InputStream self, String mimeType, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
dataclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
datafirstLine
- the index of the record where parsing should startclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(InputStream self, String mimeType, char delimiter, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
datadelimiter
- character used to split the dataclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
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 a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to be parsemimeType
- MIME type of theInputStream
datafirstLine
- the index of the record where parsing should startdelimiter
- character used to split the dataclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(InputStream self, String mimeType, org.apache.commons.csv.CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
dataformat
- format of the CSV fileclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
public static <T> T eachRecord(InputStream self, String mimeType, int firstLine, org.apache.commons.csv.CSVFormat format, Closure<T> closure) throws IOException, ToroException
Parse a flat file. This could be a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
datafirstLine
- the index of the record where parsing should startformat
- format of the CSV fileclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
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 a CSV file, 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
InputStream
will be closed for you.- Parameters:
self
- CSV file to parsemimeType
- MIME type of theInputStream
datafirstLine
- the index of the record where parsing should startsheet
- the sheet index in a spreadsheetclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
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
InputStream
will be closed for you.- Parameters:
self
- fixed-width file to parsefirstLine
- the index of the record where parsing should startindexes
- the list of indexesclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
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
InputStream
will be closed for you.- Parameters:
self
- fixed-width file to parsefirstLine
- the index of the record where parsing should startindexes
- the list of indexesclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
eachRecord
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
InputStream
will be closed for you.- Parameters:
self
- fixed-width file to parsefirstLine
- the index of the record where parsing should startindexes
- the list of indexesclosure
- the closure containing operations and optional statements- Throws:
IOException
ToroException
- Since:
- 1.0
-
-