本文共 2492 字,大约阅读时间需要 8 分钟。
在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))
def customers = new XmlSlurper().parse(new File("customers.xml")) /*对文件进行解析*/ for(customer in customers.corporate.customer){ println "${customer.@name} works for${customer.@company}"; }
参考 ,代码如下:
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"}
列出目录所有文件(包含子文件夹,子文件夹内文件) :
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 }
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/