博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Groovy操作文件
阅读量:2397 次
发布时间:2019-05-10

本文共 2492 字,大约阅读时间需要 8 分钟。

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 文件

def customers = new XmlSlurper().parse(new File("customers.xml")) /*对文件进行解析*/ for(customer in customers.corporate.customer){     println "${customer.@name} works for${customer.@company}"; }

解析 propeties 文件

参考 ,代码如下:

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')  }

转载地址:http://cbfob.baihongyu.com/

你可能感兴趣的文章
在oracle实践学习位运算 第一篇
查看>>
通过sql语句分析足彩
查看>>
java中的序列化
查看>>
使用ash分析ORA-01652问题
查看>>
生产环境sql语句调优实战第七篇
查看>>
一个oracle查询引起的bug
查看>>
通过shell来比较oracle和java中的字符串使用
查看>>
一条简单的sql语句导致的系统问题
查看>>
关于纠结的recycle pool的设置
查看>>
清华梦的粉碎读后感--论理想主义者王垠
查看>>
关于Oracle的技术问答
查看>>
增量数据丢失的原因分析(三)
查看>>
生活中的优化和向往(r11笔记第72天)
查看>>
最近的几个技术问题总结和答疑(七)
查看>>
每一次退缩之后的努力,都是极大的进步
查看>>
使用sysbench压力测试MySQL(一)(r11笔记第3天)
查看>>
MySQL 5.6, 5.7并行复制测试(r12笔记第9天)
查看>>
容灾切换中的数据库宕机问题简单分析(一)
查看>>
几年前的一次答疑解惑
查看>>
MySQL RR隔离级别的更新冲突策略
查看>>