#!/usr/bin/groovy
class CsvParser {
static void parse(String filePath, Closure closure) {
File file = new File(filePath)
file.eachLine() {line ->
def fields = line.split(";")
closure(fields)
}
}
static void usage(String name) {
println "${name} <csv file>"
}
static void main(String[] args) {
if (args.size() < 1) {
usage(CsvParser.getProtectionDomain().getCodeSource().getLocation().getPath())
return
}
parse(args[0]){fields ->
def i =0
for (field in fields) {
print "[${i}]" + field + " "
i++
}
println ""
}
}
}
Parse CSV file with Groovy
最新推荐文章于 2025-11-29 12:01:00 发布
本文介绍了一个使用Groovy编写的简单CSV文件解析器。该解析器通过传递文件路径和闭包来读取每一行,并将每行数据按分号拆分成字段。此脚本为用户提供了一种快速处理CSV数据的方法。
714

被折叠的 条评论
为什么被折叠?



