使用Groovy操作文件
来源:互联网 发布:柯尼塞格agera r知乎 编辑:程序博客网 时间:2024/05/29 13:36
1. 读文件
读文件内容
在groovy中输出文件的内容:
println new File("tmp.csv").text
上面代码非常简单,没有流的出现,没有资源关闭的出现,也没有异常控制的出现,所有的这些groovy已经搞定了。
读取每一行内容:
File file = new File('tmp.csv')assert file.name == 'tmp.csv'assert ! file.isAbsolute()assert file.path == 'tmp.csv'assert file.parent == null//使用系统默认的编码处理文件流 file.eachLine {println it } //指定处理流的编码file.eachLine("UTF-8") { println it }file.eachLine("UTF-8",10) {str,no-> println str println no }对文件中每一行的内容做处理:
file.splitEachLine("\t") { println it }//以大写行式输出文件内容 lineList = file.readLines(); liineList.each { println it.toUpperCase(); }file.filterLine {String str-> if (str.contains('code')) println str }.writeTo(new PrintWriter(System.out))
解析 xml 文件
<?xml version="1.0" encoding="UTF-8"?> <customers> <corporate> <customer name="bill gates" company="microsoft"></customer> <customer name="steve jobs" company="apple"></customer> <customer name="bill dyh" company="sun"></customer> </corporate> <consumer> <customer name="jone Doe"></customer> <customer name="jane Doe"></customer> </consumer> </customers>
def customers = new XmlSlurper().parse(new File("customers.xml")) /*对文件进行解析*/ for(customer in customers.corporate.customer){ println "${customer.@name} works for${customer.@company}"; }
解析 propeties 文件
参考 groovy: How to access to properties file?,代码如下:
def props = new Properties()new File("message.properties").withInputStream { stream -> props.load(stream) }// accessing the property from Properties object using Groovy's map notationprintln "capacity.created=" + props["capacity.created"]def config = new ConfigSlurper().parse(props)// accessing the property from ConfigSlurper object using GPath expressionprintln "capacity.created=" + config.capacity.created
另外一种方式:
def config = new ConfigSlurper().parse(new File("message.groovy").text)message.groovy 内容如下:capacity { created="x" modified="y"}
2. 操作目录
列出目录所有文件(包含子文件夹,子文件夹内文件) :
def dir = new File(dirName) if (dir.isDirectory()) { dir.eachFileRecurse { file -> println file } } dir.eachFileMatch(~/.*\.txt/) {File it-> println it.name } //使正则表达式匹配文件名 dir.eachFileMatch(FILES, ~/.*\.txt/) { File it-> println it.name }
3. 写文件
import java.io.File def writeFile(fileName) { def file = new File(fileName) if (file.exists()) file.delete() def printWriter = file.newPrintWriter() // printWriter.write('The first content of file') printWriter.write('\n') printWriter.write('The first content of file') printWriter.flush() printWriter.close()
除了
file.newPrintWriter()
可以得到一个 PrintWriter,类似方法还有 file.newInputStream()
、 file.newObjectInputStream()
等。更简洁写法:
new File(fileName).withPrintWriter { printWriter -> printWriter.println('The first content of file') }
0 0
- 使用Groovy操作文件
- Groovy 之文件操作
- Groovy的文件操作
- 使用Groovy "*“ 操作符
- Unmi 学习 Groovy 之文件操作
- Unmi 学习 Groovy 之文件操作
- maven中使用Groovy-Eclipse编译groovy文件
- SoapUI中使用Groovy脚本操作数据库 .
- SoapUI中使用Groovy脚本操作数据库
- SoapUI中使用Groovy脚本操作数据库
- Groovy 生成xml文件(使用MarkupBuilder)
- 使用groovy进行大文件外排序
- groovy中对文件,集合,测试,数据库的操作例子
- 使用groovy和java两种方式下载文件文件
- 关于使用Groovy插件化开发测试单个Groovy的文件
- Groovy基本使用(5):文件I/O 处理
- Groovy 操作xml
- Groovy 操作json
- 一个单独的条形码扫描功能实现
- vue教程集
- python pip 手动安装
- java Data、String、Long三种日期类型之间的相互转换
- BJ模拟 Goodness(凸包+三分答案)
- 使用Groovy操作文件
- 将博客搬至CSDN
- 基于DragonBoard 410c的家庭智能环保卫士——(8)室内栅格定位
- Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompa
- 汉字转拼音
- Android Studio背景图设置
- 仿百度注册页面
- bzoj 3133
- 面试的三种形式