groovy文件操作

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2021/07/groovy-wen-jian-cao.html

下载文件

/**
 * 下载文件
 * @param url
 * @param dest
 * @return
 */
static def downloadFile(String url, String dest) {
    def fos = new FileOutputStream(dest)
    def out = new BufferedOutputStream(fos)
    out << new URL(url).openStream()
    out.close()
}

downloadFile("http://jvm123.com/wp-content/uploads/2019/07/logo2-jvm123.jpg", "D:/jvm123.jpg")

遍历文件夹中的文件

/**
 * 遍历文件夹中的文件
 * @param dir
 * @param closure
 * @return
 */
static def traverseDir(File dir, @ClosureParams(value = SimpleType.class, options = "java.io.File") Closure closure) {
    dir.eachFile {file ->
        closure.call(file)
    }
    dir.eachDir {subDir ->
        traverseDir(subDir, closure)
    }
}

def dir = new File("D:/data")
traverseDir(dir, { file ->
    println file.absolutePath
})

从文件中读写数据

/*
 * 从文件中读写数据
 */
def file = new File("D:/data-file.dat")
file.withDataOutputStream {dos ->
    dos.writeBoolean(true)
    dos.writeDouble(2.16D)
    dos.writeInt(2021)
    dos.writeUTF("jvm123.com Java技术分享")
}
// 必须按照顺序读取
file.withDataInputStream {dis ->
    println dis.readBoolean()
    println dis.readDouble()
    println dis.readInt()
    println dis.readUTF()
}

def file2 = new File("D:/object-file.dat")
def map = [name: "yawn", age: 18]

file2.withObjectOutputStream {oos ->
    oos.writeObject(map)
}

file2.withObjectInputStream {ois ->
    println ois.readObject()
}

文本文件写入读取

/*
 * 文本文件写入读取
 */
def file3 = new File("D:/data.txt")
// 清空并写入
file3.write("jvm123.com")
// 追加写入
file3 << "Hello world!\n"
file3 << "World 很大,......\n"
file3 << "你怎么不去看看?"

file3.eachLine {line ->
    println line
}
def list = file3.readLines()
println list

执行外部命令

/*
 * 执行外部命令
 */
def process = "jps -l".execute()
println process.info()
println process.text
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.SimpleType

/**
 * 下载文件
 * @param url
 * @param dest
 * @return
 */
static def downloadFile(String url, String dest) {
    def fos = new FileOutputStream(dest)
    def out = new BufferedOutputStream(fos)
    out << new URL(url).openStream()
    out.close()
}

downloadFile("http://jvm123.com/wp-content/uploads/2019/07/logo2-jvm123.jpg", "D:/jvm123.jpg")

/**
 * 遍历文件夹中的文件
 * @param dir
 * @param closure
 * @return
 */
static def traverseDir(File dir, @ClosureParams(value = SimpleType.class, options = "java.io.File") Closure closure) {
    dir.eachFile {file ->
        closure.call(file)
    }
    dir.eachDir {subDir ->
        traverseDir(subDir, closure)
    }
}

def dir = new File("D:/data")
traverseDir(dir, { file ->
    println file.absolutePath
})

/*
 * 从文件中读写数据
 */
def file = new File("D:/data-file.dat")
file.withDataOutputStream {dos ->
    dos.writeBoolean(true)
    dos.writeDouble(2.16D)
    dos.writeInt(2021)
    dos.writeUTF("jvm123.com Java技术分享")
}
// 必须按照顺序读取
file.withDataInputStream {dis ->
    println dis.readBoolean()
    println dis.readDouble()
    println dis.readInt()
    println dis.readUTF()
}

def file2 = new File("D:/object-file.dat")
def map = [name: "yawn", age: 18]

file2.withObjectOutputStream {oos ->
    oos.writeObject(map)
}

file2.withObjectInputStream {ois ->
    println ois.readObject()
}

/*
 * 文本文件写入读取
 */
def file3 = new File("D:/data.txt")
// 清空并写入
file3.write("jvm123.com")
// 追加写入
file3 << "Hello world!\n"
file3 << "World 很大,......\n"
file3 << "你怎么不去看看?"

file3.eachLine {line ->
    println line
}
def list = file3.readLines()
println list


/*
 * 执行外部命令
 */
def process = "jps -l".execute()
println process.info()
println process.text

《groovy文件操作》有1个笔记:

发表评论