Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write a file in groovy

Tags:

groovy

soapui

I'm new to groovy and SOAP UI free. I'm using a groovy script to drive my test for SOAP UI.

I want to write a script that reads a file of person IDs, removes the first one, sets a property, writes the file back out without the one I just read.

Here's my first cut at it:

List pids = new ArrayList()

new File("c:/dev/pids.csv").eachLine { line -> pids.add(line) }

String pid = pids.get(0);
testRunner.testCase.setPropertyValue( "personId", pid )
pids.remove(0)

new File("c:/dev/pids.csv").withWriter { out ->
    pids.each() { aPid ->
        out.writeLine(aPid)
    }
}

The output gets displayed on SOAP UI and the file doesn't get touched. I'm lost.

like image 482
Thom Avatar asked Nov 04 '13 21:11

Thom


2 Answers

ArrayList pids = null
PrintWriter writer = null

File f = new File("c:/temp/pids.txt")

if (f.length() > 0){
   pids = new ArrayList()

   f.eachLine { line -> pids.add(line) }

   println("Item to be removed: " + pids.get(0))
   //testRunner.testCase.setPropertyValue( "personId", pid )
   pids.remove(0)

   println pids

   writer = new PrintWriter(f)
   pids.each { id -> writer.println(id) }

   writer.close()
}
else{
   println "File is empty!"
}
like image 150
Fergara Avatar answered Nov 17 '22 20:11

Fergara


def myFile = new File("newfile.txt")

def newFile = new File("newfile2.txt")

//testRunner.testCase.setPropertyValue( "personId", pid )

PrintWriter printWriter = new PrintWriter(newFile)

myFile.eachLine { currentLine, lineNumber ->

    if(lineNumber > 1 )

       printWriter.println(currentLine)
  }

printWriter.close()
like image 40
Saurabh Mittal Avatar answered Nov 17 '22 19:11

Saurabh Mittal