Groovy学习笔记

Groovy与Java的不同

1. Groovy方法可以参数可以设置默认值

class Example {
 static void sum(int a,int b = 5) {
    int c = a+b;
    println(c);
 } 
  
 static void main(String[] args) {
    sum(6,6);
 } 
}

2.文件I/0

1.复制文件
static void main(String[] args) {
      def src = new File("E:/Example.txt")
      def dst = new File("E:/Example1.txt")
      dst << src.text
   } 
2.写入文件
import java.io.File 
class Example { 
   static void main(String[] args) { 
      new File('E:/','Example.txt').withWriter('utf-8') { 
         writer -> writer.writeLine 'Hello World' 
      }  
   } 
}
3.读取文件的内容到字符串
class Example { 
   static void main(String[] args) { 
      File file = new File("E:/Example.txt") 
      println file.text 
   } 
}
4.获取目录内容
class Example { 
   static void main(String[] args) { 
      def rootFiles = new File("test").listRoots() 
      rootFiles.each { 
         file -> println file.absolutePath 
      }
   }
}

4.Groovy 列表

1.支持嵌套列表

[1,2,[3,4],5] - 嵌套列表
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容