javagroovygroovlet

Opening a local file in Groovy


I want to use a File object to read a local file in the same directory as a groovlet. However, using a relative path to the file (either "example.txt" or "./example.txt") doesn't do the trick. If I give it an absolute path (e.g., "/example.txt"), then it works.

Is there any way to get the working directory or context path of the groovlet programmatically?


Solution

  • new File("${request.getContextPath()}/example.txt")