GROOVY IO

 

import java.io.File 

class Example {

   static void main(String[] args){
      File file = new File("./abc.txt");
      println "The file ${file.absolutePath} has ${file.length()} bytes";
   }
   
}
 读文件
class Example {

   static void main(String[] args) {
      new File("./Example.txt").eachLine(){
         line -> println "line:$line";
      }
   } 
}
import java.io.File 

class Example {

   static void main(String[] args){
      new File("./file1.txt").withWriter("utf-8"){
         writer -> writer.writeLine 'Hello,World'
      }
   }
   
}
import java.io.File 

class Example {

   static void main(String[] args){
      def file = new File("./abc.txt")
      println "File? ${file.isFile()}"
      println "Directory? ${file.isDirectory()}"
   }
   
}
class Example {

   static void main(String[] args){
      def file = new File("./a")
      file.mkdir()
   }
   
}
class Example {

   static void main(String[] args){
      def file = new File("abc.txt")
      file.delete()
   }
   
}
class Example {

   static void main(String[] args){
      new File("/home/GAYUXIA/workspace/groovy").eachFile(){
         file->println file.getAbsolutePath();
      }   
   }
   
}
import java.io.File 

class Example {
   
   static void main(String[] args){
      new File("/home/GAYUXIA/workspace/groovy").eachFileRecurse(){
         file->println file.getAbsolutePath();
      }   
   }
}

 

posted @ 2025-07-07 14:03  山村放羊娃  阅读(7)  评论(0)    收藏  举报