网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么用Groovy读取本地文件的代码-创新互联

本篇内容介绍了“怎么用Groovy读取本地文件的代码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

为龙湖等地区用户提供了全套网页设计制作服务,及龙湖网站建设行业解决方案。主营业务为网站设计制作、成都网站制作、龙湖网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

下面这些包默认已经被导入了,不需要使用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 method(String arg) {
    return 1;}int method(Object arg) {
    return 2;}Object o = "Object";int result = method(o);println result

怎么用Groovy读取本地文件的代码

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

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

怎么用Groovy读取本地文件的代码

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比起来短小精悍:

怎么用Groovy读取本地文件的代码

我的文件内容:

怎么用Groovy读取本地文件的代码

输出:

怎么用Groovy读取本地文件的代码

这种方法也行:

怎么用Groovy读取本地文件的代码

完整代码:

new File('c:\\temp\\1.txt').eachLine('UTF-8') {
    println "new line->" + it
 }
 new File('c:\\temp\\1.txt').withReader('UTF-8') { reader ->
     reader.eachLine {
         println "Another line:" + it
     }
  }

“怎么用Groovy读取本地文件的代码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!


分享题目:怎么用Groovy读取本地文件的代码-创新互联
新闻来源:http://bjjierui.cn/article/cogsjp.html

其他资讯