Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Read File



Read a File in Groovy

We can read to a file in Groovy using multiple ways. Following are three most popular ways to create a file in Groovy −

  • Using FileInputStream() constructor

  • Using File.eachLine method

  • Using File.text method

Let's take a look at each of the way to read file in Groovy.

Reading a File Using FileInputStream Constructor

FileInputStream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.

Syntax

Following constructor takes a file name as a string to create an input stream object to read the file −

InputStream f = new FileInputStream("test.txt");

Example - Readng File Using FileInputStream Constructor

Following is the example to demonstrate FileInputStream to read a file from current directory −

Example.groovy

class Example {
   public static void main(String[] args) {
      try {
         byte[] bytes = [65, 66, 67, 68, 69];
         OutputStream os = new FileOutputStream("test.txt");
         for(int x = 0; x < bytes.length ; x++) {
            os.write( bytes[x] );   // writes the bytes
         }
         os.close();
     
         InputStream is = new FileInputStream("test.txt");
         int size = is.available();

         for(int i = 0; i < size; i++) {
            System.out.print((char)is.read());
         }
         is.close();
      } catch (IOException e) {
         System.out.print("Exception");
      }	
   }
}

The above code would create file test.txt and would write given numbers in binary format. Same would be read using FileInputStream and the output is printed on the stdout screen.

Output

ABCDE  

Reading File Content Using File.eachLine() Method

The following example will output all the lines 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 line of the text file is read.

Syntax

new File("test.txt").eachLine { line -> println "line : $line"; } 

Example: Reading File Content Using File.eachLine() Method

Following is the example to demonstrate File.eachLine to read a file line by line. −

class Example {
   static void main(String[] args) {
      try {
         new File('test.txt').withWriter('utf-8') { 
            writer -> { 
               writer.writeLine 'Line 1';
               writer.writeLine 'Line 2';			   
			}
         }
		 		 
		 new File('test.txt').eachLine {  
            line -> println "line : $line"; 
         }
      } catch (IOException e) {
         System.out.print("Exception");
      }	
   }
}

The above code would create file test.txt and would write given string in text format. Same would be the output on the stdout screen.

Output

line : Line 1
line : Line 2

Reading File Content Using File.text() Method

The following example will output all the lines of a text file in Groovy. The method text is in-built in the File class in Groovy to read content of the file as a string.

Syntax

File file = new File("test.txt") 
println file.text 

Example: Reading File Content Using File.text Method

Following is the example to demonstrate File.text to read a file content all at once −

class Example {
   static void main(String[] args) {
      try {
         new File('test.txt').withWriter('utf-8') { 
            writer -> { 
               writer.writeLine 'Line 1';
               writer.writeLine 'Line 2';			   
			}
         }
		 		 
         File file = new File("test.txt") 
         println file.text 
	  
      } catch (IOException e) {
         System.out.print("Exception");
      }	
   }
}

The above code would create file test.txt and would write given string in text format. Same would be the output on the stdout screen.

Output

Line 1
Line 2
Advertisements