前言:
在前面两篇学习中,对Groovy的语法基础、闭包有了一定的了解。现在继续就使用Groovy写代码来属性该语言。
多看 Groovy JDK,少些代码。同样,将Groovy DownLoad下来,可以直接查看本地文件的document,查询速度快的多。
Groovy中代码写法:脚本和类
官方文档: scripts_versus_classes
在Groovy中,同样一段代码的写法可以使用脚本、也可以使用类。
//类的写法
class Main {
static void main(String... args) {
println 'Groovy world!'
}
}
//脚本。
println 'Groovy world!';
A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method.
The generated script class will carry all methods into the script class, and assemble all script bodies into the run method.
Groovy编译器会把这种脚本编译为class文件,脚本中的内容会被放到生成类中的main()方法,方法会定义到类里。
- 每一个脚本都会生成一个 static main 函数。
- 脚本中的所有代码都会放到run函数中。
- 如果脚本中定义了函数,则函数会被定义在类中
- 变量的作用域取决于定义变量的方式,请看官方文档:variables。太多了,有点像js。
println "Groovy world"
生成代码:
import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
def run() {
println 'Groovy world!' //脚本内容被放到run()方法中
}
static void main(String[] args) { //生成main()函数
InvokerHelper.runScript(Main, args)
}
}
Groovy 使用IO流读写文件
官方文档: Working with IO
IO流相关类:
the java.io.File class :
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.htmlthe java.io.InputStream class:
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/InputStream.htmlthe java.io.OutputStream class:
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/OutputStream.htmlthe java.io.Reader class:
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/Reader.htmlthe java.io.Writer class:
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/Writer.htmlthe java.nio.file.Path class:
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/nio/file/Path.html
1.读文件
//创建文件对象
def file = new File("filePath");
//读取文件,查看API文档,熟悉用法
def aClosure = {
line ->
println line;
};
file.eachLine(aClosure);
/*
这块要说明一下:
查看API文档说明,public Object eachLine(Closure closure);
eachLine函数中要传入一个closure对象,于是上面那种写法就是正统写法。我们调用eachLine()函数,向里面传入一个闭包对象。
下面这种写法就是简写。直接省略掉括号,直接使用闭包。
Groovy中,当函数最后一个参数是闭包时,可以省略括号。
*/
//简写
file.eachLine{
line ->
println line;
};
//使用闭包操作输入流
file.withInputStream{
is ->
println is.getText(); //查看API文档,操作输入流。不用手动关闭
};
这点要尤为注意:Groovy中,当函数最后一个参数是闭包时,可以省略括号。 要不然以后在gradle中会越看看不懂。
2.写文件
//创建文件对象
def file = new File("srcfilePath");
def newFile = new File("targetFilePath");
newFile.withOutputStream{ os ->
os << file.getBytes(); //运算符重载
}
Groovy 操作XML文件
官方文档: processing xml
Groovy中提供两个工具类来操作XML文件,查看API,下载下来的api文件里:
- groovy.util.XmlParser
- groovy.util.XmlSlurper
解析XML这个功能在Android中应该会用,因为毕竟要操作manifest.xml文件。
示例,两种方式都能解析:
def text = '''
<some>
<technology name="groovyProperty">
<name>GroovyNode</name>
</technology>
</some>
'''
//使用XmlSlurper解析
def some = new XmlSlurper().parseText(text); //根节点
assert some instanceof groovy.util.slurpersupport.GPathResult
println some.technology.name;
println some.technology[0].@name;
//使用XmlParser解析,需要调动text()方法
def list = new XmlParser().parseText(text)
assert list instanceof groovy.util.Node
println list.technology.name.text() == 'GroovyNode'
结语:
Groovy本身是一门基于JVM的语言,就像是一个新语言一样。这里仅仅学习、介绍了和gradle相关的Groovy使用。之后开始学习gradle,相信在深入学习gradle过程中会返回来加深对Groovy的理解和使用。
文中相关代码在 github ioLearn 上。
再次感谢前辈们的学习总结和经验,让自己少走了很多弯路。