Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Creating File



Create File in Groovy

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

  • Using FileOutputStream() constructor

  • Using File.createNewFile() method

  • Using File() constructor

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

Create File Using FileOutputStream Constructor

FileOutputStream class is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.

Following constructor takes a file name as a string to create an output stream object to write the file −

Syntax

OutputStream os = new FileOutputStream("test.txt");

Example - Create File Using FileOutputStream Constructor

Following is the example to demonstrate FileOutputStream to create a file in current directory −

Example.groovy

class Example {
   static void main(String[] args) {  
      try {
         byte[] bWrite = [65, 66, 67, 68, 69];
         OutputStream os = new FileOutputStream("test.txt");
         for(int x = 0; x < bWrite.length ; x++) {
            os.write( bWrite[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 the output on the stdout screen.

Output

ABCDE  

Example - Create File Using File.createNewFile() Method

File.createNewFile() method allows to create a file in given location or in current directory as follows −

Syntax

File file = new File("test1.txt");

//Create the file
if (file.createNewFile()) { 
   System.out.println("File is created!");
} else {
   System.out.println("File already exists.");
} 

Example - Create File Using File.createNewFile() Method

Following is the example to demonstrate File to create a file in given directory using createNewFile() method −

Example.groovy

class Example {
   static void main(String[] args) {  
      try {
          File file = new File("test1.txt");
          
          //Create the file
          if (file.createNewFile()) { 
             System.out.println("File is created!");
          } else {
             System.out.println("File already exists.");
          } 
          
          // Write Content
          FileWriter writer = new FileWriter(file);
          writer.write("Test data");
          writer.close();
          
          // read content
          FileReader reader = new FileReader(file);
          
          int c;
          while ((c = reader.read()) != -1) {
        	  char ch = (char) c;
              System.out.print(ch);
          }
      } catch (IOException e) {
         System.out.print("Exception");
      }	
   }
}

Output

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.

File is created!
Test data

Create File Using File() Constructor

File() is a easier method to create a file and write content to a file in same command as shown below −

Syntax

new File('test.txt').withWriter('utf-8') { writer -> writer.writeLine 'Hello World!' }

Example: Create File Using File() Constructor

Following is the example to demonstrate File constructor to create a file in given directory −

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

Output

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

Hello World!
Advertisements