使用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