1. 读文件

读文件内容

在groovy中输出文件的内容:

println new File("tmp.csv").text

上面代码非常简单,没有流的出现,没有资源关闭的出现,也没有异常控制的出现,所有的这些groovy已经搞定了。

读取每一行内容:

  1. File file = new File(‘tmp.csv’)
  2. assert file.name == ‘tmp.csv’
  3. assert ! file.isAbsolute()
  4. assert file.path == ‘tmp.csv’
  5. assert file.parent == null
  6. //使用系统默认的编码处理文件流
  7. file.eachLine {println it }
  8. //指定处理流的编码
  9. file.eachLine(“UTF-8”) { println it }
  10. file.eachLine(“UTF-8”,10) {str,no->
  11. println str
  12. println no }

对文件中每一行的内容做处理:

  1. file.splitEachLine(“\t”) { println it }
  2. //以大写行式输出文件内容
  3. lineList = file.readLines();
  4. liineList.each {
  5. println it.toUpperCase();
  6. }
  7. file.filterLine {String str->
  8. if (str.contains(‘code’))
  9. println str
  10. }.writeTo(new PrintWriter(System.out))

解析 xml 文件

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <customers>
  3. <corporate>
  4. <customer name=“bill gates” company=“microsoft”></customer>
  5. <customer name=“steve jobs” company=“apple”></customer>
  6. <customer name=“bill dyh” company=“sun”></customer>
  7. </corporate>
  8. <consumer>
  9. <customer name=“jone Doe”></customer>
  10. <customer name=“jane Doe”></customer>
  11. </consumer>
  12. </customers>

 

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

解析 propeties 文件

参考 groovy: How to access to properties file?,代码如下:

  1. def props = new Properties()
  2. new File(“message.properties”).withInputStream {
  3. stream -> props.load(stream)
  4. }
  5. // accessing the property from Properties object using Groovy’s map notation
  6. println “capacity.created=” + props[“capacity.created”]
  7. def config = new ConfigSlurper().parse(props)
  8. // accessing the property from ConfigSlurper object using GPath expression
  9. println “capacity.created=” + config.capacity.created

另外一种方式:

  1. def config = new ConfigSlurper().parse(new File(“message.groovy”).text)
  2. message.groovy 内容如下:
  3. capacity {
  4. created=“x”
  5. modified=“y”
  6. }

 

2. 操作目录

列出目录所有文件(包含子文件夹,子文件夹内文件) :

  1. def dir = new File(dirName)
  2. if (dir.isDirectory()) {
  3. dir.eachFileRecurse { file ->
  4. println file
  5. }
  6. }
  7. dir.eachFileMatch(~/.*\.txt/) {File it-> println it.name } //使正则表达式匹配文件名
  8. dir.eachFileMatch(FILES, ~/.*\.txt/) { File it-> println it.name }

 

3. 写文件

  1. import java.io.File
  2. def writeFile(fileName) {
  3. def file = new File(fileName)
  4. if (file.exists())
  5. file.delete()
  6. def printWriter = file.newPrintWriter() //
  7. printWriter.write(‘The first content of file’)
  8. printWriter.write(‘\n’)
  9. printWriter.write(‘The first content of file’)
  10. printWriter.flush()
  11. printWriter.close()

除了  file.newPrintWriter()  可以得到一个 PrintWriter,类似方法还有  file.newInputStream() 、 file.newObjectInputStream() 等。

更简洁写法:

  1. new File(fileName).withPrintWriter { printWriter ->
  2. printWriter.println(‘The first content of file’)
  3. }