我是靠谱客的博主 娇气香氛,这篇文章主要介绍Gradle自动化构建(七) Groovy file,现在分享给大家,希望可以做个参考。

###file
def file = new File(’…/…/hjGroovy.iml’)
//file.eachLine { println it } // 读取文件每一行 , ResourceGroovyMethods 中提供的 方法
String content = file.getText() // 获取文件中所有字符
def list = file.readLines() // 读取文件每行,返回每行字符串的集合

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def reader = file.withReader { // 获取Reader对象,进行文件操作 reader -> char[] buf = new char[100] reader.read(buf) // 读取文件中的前 100 个字符 return buf } //println reader // 打印 // 复制文本文件,二进制文件的复制? file.withDataInputStream {} ? file.withDataOutputStream {} ? def copy(String srcPath, String dstPath) { try { def srcFile = new File(srcPath) def dstFile = new File(dstPath) if (!dstFile.exists()) dstFile.createNewFile() srcFile.withReader { reader -> def lines = reader.readLines() dstFile.withWriter { writer -> // 获取Writer对象,可以对文件进行写的操作 , groovy会自动关闭流 lines.each { line -> writer.append(line+'rn') } } } } catch (Exception e) { } } copy('E:\gradle\a.txt', 'E:\gradle\copy.txt') // 序列化与反序列化 class ABC { String name int age } def saveObject(Object object, String path) { try { //首先创建目标文件 def desFile = new File(path) if (!desFile.exists()) desFile.createNewFile() desFile.withObjectOutputStream { out -> out.writeObject(object) } } catch (Exception e) { } } 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() println obj==null } } catch (Exception e) { } return obj } def po = new ABC(name: 'abc', age: 26) saveObject(po, 'E:\gradle\abc.bin') def ret = (ABC) readObject('E:\gradle\abc.bin') println "name is ${ret.name}, age is ${ret.age}"

最后

以上就是娇气香氛最近收集整理的关于Gradle自动化构建(七) Groovy file的全部内容,更多相关Gradle自动化构建(七)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部