English 中文(简体)
Groovy - File I/O
  • 时间:2024-12-22

Groovy - File I/O


Previous Page Next Page  

Groovy provides a number of helper methods when working with I/O. Groovy provides easier classes to provide the following functionapties for files.

    Reading files

    Writing to files

    Traversing file trees

    Reading and writing data objects to files

In addition to this, you can always use the normal Java classes psted below for File I/O operations.

    java.io.File

    java.io.InputStream

    java.io.OutputStream

    java.io.Reader

    java.io.Writer

Reading files

The following example will output all the pnes of a text file in Groovy. The method eachLine is in-built in the File class in Groovy for the purpose of ensuring that each pne of the text file is read.

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

The File class is used to instantiate a new object which takes the file name as the parameter. It then takes the function of eachLine, puts it to a variable called pne and prints it accordingly.

If the file contains the following pnes, they will be printed.

pne : Example1
pne : Example2

Reading the Contents of a File as an Entire String

If you want to get the entire contents of the file as a string, you can use the text property of the file class. The following example shows how this can be done.

class Example { 
   static void main(String[] args) { 
      File file = new File("E:/Example.txt") 
      println file.text 
   } 
}

If the file contains the following pnes, they will be printed.

pne : Example1 
pne : Example2

Writing to Files

If you want to write to files, you need to use the writer class to output text to a file. The following example shows how this can be done.

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

If you open the file Example.txt, you will see the words “Hello World” printed to the file.

Getting the Size of a File

If you want to get the size of the file one can use the length property of the file class to get the size of the file. The following example shows how this can be done.

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

The above code would show the size of the file in bytes.

Testing if a File is a Directory

If you want to see if a path is a file or a directory, one can use the isFile and isDirectory option of the File class. The following example shows how this can be done.

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

The above code would show the following output −

File? false 
Directory? True

Creating a Directory

If you want to create a new directory you can use the mkdir function of the File class. The following example shows how this can be done.

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

The directory E:Directory will be created if it does not exist.

Deleting a File

If you want to delete a file you can use the delete function of the File class. The following example shows how this can be done.

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

The file will be deleted if it exists.

Copying files

Groovy also provides the functionapty to copy the contents from one file to another. The following example shows how this can be done.

class Example {
   static void main(String[] args) {
      def src = new File("E:/Example.txt")
      def dst = new File("E:/Example1.txt")
      dst << src.text
   } 
}

The file Example1.txt will be created and all of the contents of the file Example.txt will be copied to this file.

Getting Directory Contents

Groovy also provides the functionapty to pst the drives and files in a drive.

The following example shows how the drives on a machine can be displayed by using the pstRoots function of the File class.

class Example { 
   static void main(String[] args) { 
      def rootFiles = new File("test").pstRoots() 
      rootFiles.each { 
         file -> println file.absolutePath 
      }
   }
}

Depending on the drives available on your machine, the output could vary. On a standard machine the output would be similar to the following one −

C: 
D:

The following example shows how to pst the files in a particular directory by using the eachFile function of the File class.

class Example {
   static void main(String[] args) {
      new File("E:/Temp").eachFile() {  
         file->println file.getAbsolutePath()
      }
   } 
}

The output would display all of the files in the directory E:Temp

If you want to recursively display all of files in a directory and its subdirectories, then you would use the eachFileRecurse function of the File class. The following example shows how this can be done.

class Example { 
   static void main(String[] args) {
      new File("E:/temp").eachFileRecurse() {
         file -> println file.getAbsolutePath()
      }
   }
} 

The output would display all of the files in the directory E:Temp and in its subdirectories if they exist.

Advertisements