Apurb
Apurb

Reputation: 1

Groovy: Read CSV File without using external library, put column into array

My CSV file:

Created Date,User Name,Email Address,First Name,Last Name
?20-05-2020,Test1,[email protected],Test,1
20-05-2020,test2,[email protected],Test,2
20-05-2020,Test3,[email protected],Test,3

I want to get all the email address and put it in Array + Without using external reference/ Library.

Please help.

Upvotes: 0

Views: 678

Answers (2)

Stephen Smith
Stephen Smith

Reputation: 55

Not using a library is a recipe for disaster. There will almost end up being entries that contain commas or whatever the actual separator is, and Excel is notorious for creating corrupted CSVs (non-standard is what it Excels at)

The beauty of Groovy scripts is that they are standalone files that can reach out to the world without requiring any other infrastructure except Groovy being installed.

Using Grape, you can automatically indicate that you want to use a 3rd party library and not worry about jar files, classpaths or installing dependencies.

@Grab(group='org.apache.commons', module='commons-csv', version='1.8')

Now you can import the relevant CSV classes you want and just use them.

Upvotes: 1

ou_ryperd
ou_ryperd

Reputation: 2133

This will help you parse the csv without a library:

new File("src/data.csv").splitEachLine(",") { line ->
    println "CreatedDate: ${line[0]}; UserName: ${line[1]}"
}

For adding the values to a List, you can do it in the normal way inside the closure.

Upvotes: 0

Related Questions