free seo tool

How to Create, Write and Append a File using Groovy Script

The below code will explain how to create a file using groovy script in soapUI Groovy script editor.//Define the name of the file.
def fileName = "test1.txt"
// Defining a file handler/pointer to handle the file.
def inputFile = new File("C:\\"+fileName)
// Check if a file with same name exisits in the folder.
if(inputFile.exists())
{
// if a file exisits then it will print the message to the log.
log.info("A file named " + fileName + " already exisits in the same folder")
}
else
{
//else it will create a file and write the text "Hello World !"
inputFile.write("Hello World !")


}


If we don't use inputFile.exists() and check if the file is there or not, it will overwrite the contents of the file and recreate a new one with same name.
If we want to append text to an existing file, replace "inputFile.write("Hello World !")" with "inputFile.append("Hello World !")".

Software Testing Tutorials
9 comments:
  • Suresh Sanga says:
    March 14, 2012

    Good

  • Sudeesh says:
    March 14, 2012

    Thank you Suresh..

  • Anonymous says:
    May 07, 2014

    Its awesome. Very very helpful in SOAPUI testing.

  • Unknown says:
    August 27, 2014

    how to write request file with run time data. as I mentioned variable for few tags directly in request file. so when I writr im only seeing the variable

  • Anonymous says:
    November 16, 2015

    Thanks. This was helpful.

  • Anonymous says:
    May 03, 2016

    Thank you!

  • DANIELO says:
    May 13, 2016

    Very useful,thanks

  • Hussain Vhora says:
    March 06, 2019

    This is really good. It will help anyone to start with. Thanks for your efforts

  • Unknown says:
    August 25, 2021

    Thanks

Post a Comment

This is a new website mainly for SOA Testers using soapUI. Let me know your thoughts/ suggestions.