Class 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 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 parse
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        closure - 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 parse
        delimiter - character used to split the data
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        delimiter - character used to split the data
        closure - 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 parse
        format - format of the CSV file
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        format - format of the CSV file
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        sheet - the sheet index in a spreadsheet
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        firstLine - the index of the record where parsing should start
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        delimiter - character used to split the data
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        firstLine - the index of the record where parsing should start
        delimiter - character used to split the data
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        format - format of the CSV file
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        firstLine - the index of the record where parsing should start
        format - format of the CSV file
        closure - 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 parse
        mimeType - MIME type of the InputStream data
        firstLine - the index of the record where parsing should start
        sheet - the sheet index in a spreadsheet
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        indexes - the list of indexes
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        indexes - the list of indexes
        closure - 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 parse
        firstLine - the index of the record where parsing should start
        indexes - the list of indexes
        closure - the closure containing operations and optional statements
        Throws:
        IOException
        ToroException
        Since:
        1.0