Groovy快速入门-12-Groovy如何读文件内容和写入文件

文件读写操作,在编程中经常需要去写的代码,同样在pipeline代码中,一些测试验证的代码也需要去读文件来判断下一步的动作,本篇来介绍如何读文件的groovy代码。然后简单介绍如何写入文件,写入文件比较简单。

 

1.数据准备

为了演示读文件,我在groovy项目根目录下,新建一个Data.txt的文件,内容如下。

在计算机科学中,闭包(英语:Closure),
又稱词法闭包(Lexical Closure)
或函數閉包(function closures),
是引用了自由变量的函数。
这个被引用的自由变量将和这个函数一同存在,
即使已经离开了创造它的环境也不例外。
所以,有另一种说法认为闭包是由函数和与其相关的
引用环境组合而成的实体。

准备工作就好了,开始用groovy来以各种方式去读文件内容。

 

2.以字符串方式读取

package com.anthony.demo

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 以字符串方式全部读取
println file.text

打印结果

在计算机科学中,闭包(英语:Closure),
又稱词法闭包(Lexical Closure)
或函數閉包(function closures),
是引用了自由变量的函数。
这个被引用的自由变量将和这个函数一同存在,
即使已经离开了创造它的环境也不例外。
所以,有另一种说法认为闭包是由函数和与其相关的
引用环境组合而成的实体。

3.读取到list容器中

package com.anthony.demo

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 读取到list
def list = file.collect { it }
println list

打印效果是 [xxx这些内容]

 

4.读取到字符串数组中

package com.anthony.demo

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 读取到数组
def arr = file as String[]
println arr

 

5.一行一行读取

5.1 每行读取到一个list

package com.anthony.demo

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 每行读取到一个list中
def list = file.readLines()
println list

5.2 一行一行读取,一行是String

package com.anthony.demo

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 一行一行读取
file.eachLine {line ->
	println line
}

 

6.二进制文件读取

有些文件是只能通过二进制读取,例如图片,音频等

// 文件位置
def filePath = "Data.txt"
File file = new File(filePath)
// 二进制文件读取
def byte[] contents = file.bytes
println contents

读取效果

[-44, -38, -68, -58, -53, -29, -69, -6, -65, -58, -47, -89, -42, -48, -93, -84, -79, -43, -80, -4, -93, -88, -45, -94, -45, -17, -93, -70, 67, 108, 111, 115, 117, 114, 101, -93, -87, -93, -84, 13, 10, -45, -42, -73, 81, -76, -54, -73, -88, -79, -43, -80, -4, -93, -88, 76, 101, 120, 105, 99, 97, 108, 32, 67, 108, 111, 115, 117, 114, 101, -93, -87, 13, 10, -69, -14, -70, -81, -108, -75, -23, 93, -80, -4, -93, -88, 102, 117, 110, 99, 116, 105, 111, 110, 32, 99, 108, 111, 115, 117, 114, 101, 115, -93, -87, -93, -84, 13, 10, -54, -57, -46, -3, -45, -61, -63, -53, -41, -44, -45, -55, -79, -28, -63, -65, -75, -60, -70, -81, -54, -3, -95, -93, 13, 10, -43, -30, -72, -10, -79, -69, -46, -3, -45, -61, -75, -60, -41, -44, -45, -55, -79, -28, -63, -65, -67, -85, -70, -51, -43, -30, -72, -10, -70, -81, -54, -3, -46, -69, -51, -84, -76, -26, -44, -38, -93, -84, 13, 10, -68, -76, -54, -71, -46, -47, -66, -83, -64, -21, -65, -86, -63, -53, -76, -76, -44, -20, -53, -4, -75, -60, -69, -73, -66, -77, -46, -78, -78, -69, -64, -3, -51, -30, -95, -93, 13, 10, -53, -7, -46, -44, -93, -84, -45, -48, -63, -19, -46, -69, -42, -42, -53, -75, -73, -88, -56, -49, -50, -86, -79, -43, -80, -4, -54, -57, -45, -55, -70, -81, -54, -3, -70, -51, -45, -21, -58, -28, -49, -32, -71, -40, -75, -60, 13, 10, -46, -3, -45, -61, -69, -73, -66, -77, -41, -23, -70, -49, -74, -8, -77, -55, -75, -60, -54, -75, -52, -27, -95, -93]

7.写入文件

先来看看,写入两行内容到一个文件。

package com.anthony.demo

// 文件位置
def filePath = "Data1.txt"
File file = new File(filePath)
// 写入文件
file.write("This is line1")
file << "\nThis is line2"
println file.text

会写入两行内容,如果接着运行下面代码,会清空上面两行再写入新的内容

package com.anthony.demo

// 文件位置
def filePath = "Data1.txt"
File file = new File(filePath)
// 写入文件
file.write("This is line1")
file << "\nThis is line2"
file.text = "This is line3"
println file.text

所以,如果要使用file.text="xxx"的时候,一定要想好是不是要覆盖前面已存在的内容。

如果不想覆盖,就用append方法

package com.anthony.demo

// 文件位置
def filePath = "Data1.txt"
File file = new File(filePath)
// 写入文件
file.write("This is line1")
file << "\nThis is line2"
file.append("\nThis is line3")
println file.text

Groovy的入门教程暂时更新到这里,后面的知识是OOP,也就是类和对象以及继承,接口等知识。就我所知,在CI CD pipeline中,我们使用groovy语法有了这些内容是足够,这些groovy语法加上jenkins上pipeline的语法和插件提供的那些方法,足以支撑你完成相关pipeline代码的编写。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值