Groovy读写文件


/**
 * @author :ZackZhou
 * @date :Created in 2020/9/15 2:04 PM
 * @description :
 * @modified By:
 * @version:
 */

//write file and read content


//short file
def short_file = new File("./practice_07.groovy")

def content = short_file.text

println(content)


//large file
def large_file = new File("./practice_07.groovy")
large_file.withReader {reader ->
    def line
    while((line = reader.readLine()) != null)
        println(line)
}


//another style
large_file.eachLine {line -> println(line)}


//write content
def w_file = new File("./content.txt")
w_file.withWriter { writer ->
    100.times {writer.append("Hello \n")}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM