How to check file exists in Groovy script

mona picture mona · Nov 2, 2018 · Viewed 14.2k times · Source

Workbook aWorkBook = Workbook.getWorkbook(new File("C:\Users\Response.xls"));
WritableWorkbook workbook1 = Workbook.createWorkbook(new File("C:\Users\Responses.xls"), aWorkBook);


I am doing DATA DRIVEN TESTING using Groovy script in SOAP UI. Above is the part of the code where it is creating new file and then writing PASS or FAIL result to that excel. Suppose if have 5 testcases then I want to create new file only once in the first loop(1st testcase) then for next loop its should open the existing file.But currently it is creating new file in every loop and overriding the data and PASS result is displaying only for the last testcase. Can anyone help to resolve this?

Answer

Evgeny Smirnov picture Evgeny Smirnov · Nov 2, 2018

There is exists() method in File:

File file = new File("/path-to-file")
println file.exists()