当前位置 博文首页 > 残缺的歌的专栏:Groory(一) 语法篇---极速入门总结

    残缺的歌的专栏:Groory(一) 语法篇---极速入门总结

    作者:[db:作者] 时间:2021-09-06 16:18

    这篇文章主要针对有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.

          int X= 6;   
          def _Name = "Joe"; 

    ?

    二、范围操作符

    ?.. 代表一个范围

    class Example { 
       static void main(String[] args) { 
          def range= 5..10; 
          println(range); 
          println(range.get(2)); 
       } }


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

    def someMethod(parameter1, parameter2= 0, parameter3= 0) { 
       // Method code goes here} 

    ?

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

    //读
    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() //删除文件
    
    //复制文件
          def src= new File("E:/Example.txt")
          def dst= new File("E:/Example1.txt")
          dst<< src.text
    
    //获取当前文件夹列表
    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()
          }


    ?

    cs