groovy 文件处理

1. java文件处理

java文件处理包括

  • 节点流,InputStream,OutputStream及其子类
  • 处理流,Reader,Writer及其子类

2. groovy文件处理

所有java对文件的处理类,groovy都可以使用。
groovy拓展了许多更加快捷和强大的方法。
groovy把文件看做是一种资源,提供的方法都是通过ResourceGroovyMethods提供的。

2.1 遍历文件内容

2.1.1 readLine()方法

groovy提供了eachLine()方法,可以遍历文件中每行内容:

def file = new File('../../GroovySpecIflcation.iml')

file.eachLine { line ->
    println line
}

2.1.2 getText()方法

groovy提供了另外一种方法获取文件的内容,这个方法就是getText():

def file = new File('../../GroovySpecIflcation.iml')

def text = file.getText()
println text

2.1.3 readLines()方法

也可以通过readLines()方法获取,这个方法返回的是List,它会把文件每一行内容都当做一个元素存储在List中返回。

def result = file.readLines()

2.1.4 withReader()方法

groovy通过withReader()方法可以读取某一部分的内容,与之对应的方法是withWriter()方法,提供写入一部分内容功能。

//读取文件部分内容
def reader = file.withReader { reader ->
    char[] buffer = new char[100]
    reader.read(buffer)
    return buffer
}
println reader

2.2 实现复制功能

通过withReader()和withWriter()方法实现复制文件功能:

def result = copy('../../GroovySpecIflcation.iml'
        , '../../GroovySpecification2.iml')
println result

def copy(String sourcePath, String destationPath) {
    try {
        //首先创建目标文件
        def desFile = new File(destationPath)
        if (!desFile.exists()) {
            desFile.createNewFile()
        }

        //开始copy
        new File(sourcePath).withReader { reader ->
            def lines = reader.readLines()
            desFile.withWriter { writer ->
                lines.each { line ->
                    writer.append(line + "\r\n")
                }
            }
        }
        return true
    } catch (Exception e) {
        e.printStackTrace()
    }
    return false
}

groovy提供的文件操作方法会默认帮助处理关闭流,所以并不需要我们自己去关闭相关的文件流。

2.3 对象读写

groovy使用withObjectOutputStream和withObjectInputStream方法实现对象的读写,对象存储是按照字节存储的。

def person = new Person(name: 'Qndroid', age: 26)
saveObject(person, '../../person.bin')

def result = (Person) readObject('../../person.bin')
println "the name is ${result.name} and the age is ${result.age}"

def saveObject(Object object, String path) {
    try {
        //首先创建目标文件
        def desFile = new File(path)
        if (!desFile.exists()) {
            desFile.createNewFile()
        }
        desFile.withObjectOutputStream { out ->
            out.writeObject(object)
        }
        return true
    } catch (Exception e) {
    }
    return false
}

def readObject(String path) {
    def obj = null
    try {
        def file = new File(path)
        if (file == null || !file.exists()) return null
        //从文件中读取对象
        file.withObjectInputStream { input ->
            obj = input.readObject()
        }
    } catch (Exception e) {

    }
    return obj
}
Person.groovy
class Person implements Serializable {

    String name

    Integer age

    def increaseAge(Integer years) {
        this.name += years
    }

    def invokeMethod(String name, Object args) {
        return "the methos is ${name}, the params is ${args}"
    }

    def methodMissing(String name, Object args){
        return "the methos is ${name} is missing"
    }
}


 

发布了70 篇原创文章 · 获赞 16 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq282330332/article/details/89172631