Skip to content

Instantly share code, notes, and snippets.

@rmorenobello
Forked from kellyrob99/openCsv.groovy
Last active August 29, 2015 14:16
Show Gist options
  • Save rmorenobello/503ac823565c43a50df5 to your computer and use it in GitHub Desktop.
Save rmorenobello/503ac823565c43a50df5 to your computer and use it in GitHub Desktop.
@Grab(group = 'net.sf.opencsv', module = 'opencsv', version = '2.3')
import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser
import au.com.bytecode.opencsv.CSVWriter
def TEST_FILE_NAME = 'test.csv'
def TEST_OUTPUT_FILE_NAME = 'testOut.csv'
List<String[]> rows = new CSVReader(new FileReader(new File(TEST_FILE_NAME)), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_QUOTE_CHARACTER, 1).readAll()
// to filter the resultSet:
def rowsOver100 = rows.findAll {it[1].toInteger() > 100}
File output = new File(TEST_OUTPUT_FILE_NAME)
if (output.exists()) { output.delete() }
// Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
// Returns false if there already existed a file with the same name.
// output.createNewFile() // El withWriter ya crea el archivo si detecta que no existe
// TODO: writer en ANSI (para Excel)
output.withWriter { writer ->
new CSVWriter(writer).writeAll(rowsOver100)
}
//println rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment