.net 本地文件管理 代码_用Groovy读取本地文件的代码

e8936a790884df8899e0c70061fc630e.png

下面这些包默认已经被导入了,不需要使用import再次显式导入:

  • java.io.*
  • java.lang.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.*
  • java.util.*
  • groovy.lang.*
  • groovy.util.*

Groovy的运行时方法调用抉择

运行时,Groovy根据参数类型决定具体哪一个方法被执行。而Java恰恰相反,被调用的方法根据参数类型,在编译期间就已经定下来了。

In Groovy, the methods which will be invoked are chosen at runtime. This is called runtime dispatch or multi-methods. It means that the method will be chosen based on the types of the arguments at runtime. In Java, this is the opposite: methods are chosen at compile time, based on the declared types.

下列代码的打印结果是1:

int 

92a9b6283f12067ee9a1ffc13d35fac7.png

在Groovy里,成对的大括号是声明闭包用的,因此定义数组的语法改用中括号:

int[] array = [1, 2, 3]

078e240c34f34eb01e761b16e360fb11.png

Groovy里的闭包,it为默认参数:

Closures may have 1...N arguments, which may be statically typed or untyped. The first parameter is available via an implicit untyped argument named it if no explicit arguments are named. If the caller does not specify any arguments, the first parameter (and, by extension, it) will be null.
That means that a Groovy Closure will always have at least one argument, called it (if not specified otherwise) and it will be null if not given as a parameter.

看个用Groovy读取本地文件内容的代码,和Java比起来短小精悍:

c2e721c716d20fa77dd90bca5d88d06d.png

我的文件内容:

6bf14c0929a0d279addcab36538c309f.png

输出:

82fb9ce9a86ea73ab05c14bbe5aa1f96.png

这种方法也行:

503d6e68a0ca0282272f6eb487f39c15.png

完整代码:

new 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

13105159b2202b97092c3060f6feb28f.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值