我是靠谱客的博主 斯文可乐,这篇文章主要介绍Groory(一) 语法篇---极速入门总结,现在分享给大家,希望可以做个参考。

这篇文章主要针对有java基础很扎实,学习能力比较强的同学进行学习。因为groovyjava同根,所以这里只列出与java不同的一部分,力求精简!避免啰嗦重复。

一、声明方式:

Variables in Groovy can be defined in two ways − using the native syntax for the data type or the next is by using the def keyword.

复制代码
1
2
int X= 6; def _Name = "Joe";

 

二、范围操作符

 .. 代表一个范围

复制代码
1
2
3
4
5
6
class Example { static void main(String[] args) { def range= 5..10; println(range); println(range.get(2)); } }


三、方法中可以设置默认参数值

复制代码
1
2
def someMethod(parameter1, parameter2= 0, parameter3= 0) { // Method code goes here}

 

四、文件IO一些常用的操作

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//读 new File("E:/Example.txt").eachLine{ line-> println"line : $line"; } File file= new File("E:/Example.txt") println file.text //读文件大小 File file= new File("E:/Example.txt") println"The file ${file.absolutePath} has ${file.length()} bytes" // 写操作 new File('E:/','Example.txt').withWriter('utf-8') { writer-> writer.writeLine'Hello World' } file.isFile() //测试是否文件 file.isDirectory() //测试是否是文件夹 file.mkdir() //创建文件夹 file.delete() //删除文件
复制代码
1
2
3
4
5
//复制文件 def src= new File("E:/Example.txt") def dst= new File("E:/Example1.txt") dst<< src.text
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
//获取当前文件夹列表 def rootFiles= new File("test").listRoots() rootFiles.each{ file-> println file.absolutePath } new File("E:/Temp").eachFile() { file->println file.getAbsolutePath() } new File("E:/temp").eachFileRecurse() { file-> println file.getAbsolutePath() }


 

最后

以上就是斯文可乐最近收集整理的关于Groory(一) 语法篇---极速入门总结的全部内容,更多相关Groory(一)内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部