为了账号安全,请及时绑定邮箱和手机立即绑定

Gradle系列之二 Groovy对文件的操作

标签:
Android

Groovy对文件的操作

对文件的遍历
假设文件的原始内容为:

hello,world
这里是北京
andorid and ios are good system

第一种方法:使用 eachLine()

//1.1 new 一个Filedef file = new File(filepath)//1.2 groovy对文件的遍历file.eachLine {
    //打印每一行内容
    line -> println line
}//输出hello,world
这里是北京
andorid and ios are good system

第二种方法:使用File的getText()

def content = file.getText()
println content//输出hello,world
这里是北京
andorid and ios are good system

是不是更简单,直接调用一个方法就OK了,比Java操作文件要简单太多了吧

第三种方法:使用 file.readLines()方法

def list = file.readLines()list.collect {
    println it
}

println "文件有" + list.size() + "行"//输出hello,world
这里是北京
andorid and ios are good system
文件有3行

是不是很方便,readLines()函数直接把文件内容以行为单位读取到一个List中,这样操作就更方便了

第四种方法:读取文件部分内容

//读取前20个字符def reader = file.withReader {
    reader ->        char[] buffer = new char[20]
        reader.read(buffer)        return buffer
}

println reader//输出hello,world
这里是北京
an

如何拷贝文件?

我们写一个方法,把刚才的文件拷贝到另一个文件中去,代码如下:

def copy(String sourcePath, String destPath) {    try {        //1 创建目标文件
        def destFile = new File(destPath)        if (!destFile.exists()) {
            destFile.createNewFile()
        }        //2 开始拷贝
        new File(sourcePath).withReader { reader ->            def lines = reader.readLines()
            destFile.withWriter { writer ->
                lines.each {                    //把每一行都写入到目标文件中
                    line -> writer.append(line+"\r\n")
                }
            }
        }        return true
    } catch (Exception e) {        return false
    }
}

读写对象
有时候我们会有这样的需求,需要把我们的bean对象写入到文件中,用到的时候再读出来,下面我们就来实现这样的功能,代码如下:

//将一个对象写入到文件中def saveObject(Object object, String path) {    try {        //1 首先创建目标文件
        def destFile = new File(path)        if (!destFile.exists()) {
            destFile.createNewFile()
        }

        destFile.withObjectOutputStream { out ->
            out.writeObject(object)
        }        return true
    } catch (Exception e) {
    }    return false;
}//从一个文件中读到beandef readObject(String path) {    def obj = null
    try {        //1 先判断文件是否存在
        def file = new File(path)        if (!file.exists()) {            return null
        }        //2 从文件中读取对象
        file.withObjectInputStream { reader ->
            obj = reader.readObject();
        }        return obj
    } catch (Exception e) {
    }    return null}

Groovy对xml文件的操作

/**
     test.xml 文件的内容如下:

     <langs type="current">
         <language1>Java</language1>
         <language2>Groovy</language2>
         <language3>JavaScript</language3>
     </langs>
 *///一行代码就解析了xmldef langs = new XmlParser().parse("test.xml")//打印出node的属性println langs.attribute('type')//对xml文件的遍历langs.each {    println it.text()
}//输出current
Java
Groovy
JavaScript

以上就是groovy对文件的操作

原文出处:https://www.cnblogs.com/start1225/p/9943541.html  

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消