FileWriter creates file outside of Jenkins workspace?

I’m trying to use FileWriter to create a CSV file, like this:
CSVPrinter printer = new CSVPrinter(new FileWriter(“MyFile.csv”), CSVFormat.EXCEL)

This works, except the file is created outside my workspace, in the folder which contains my Jenkins home folder. Why is it creating it there? I thought maybe I had changed my working directory, but if I put this immediately before the above line, then "groovy1.txt’ is created where I expect - in the workspace folder:
writeFile file: ‘groovy1.txt’, text: ‘This is a test text file.’

Is there something about FileWriter or CSVPrinter which would cause the file to be created outside of my Jenkins home folder?

Generally don’t access your filesystem using raw java libraries as they are not agent aware

I’m pretty sure it’s come up in the forums already but either don’t have that complex logic directly in your Jenkinsfile (which runs on the controller), have it in a script you can call out to. That’ll run it directly on your agent.

Failing that, output a string from your csv writer and use writeFile pipeline step that is agent aware.

Thank you for the reply. I don’t know that I have any agents - sorry, I am pretty new to Jenkins. This is running in a shared library groovy file however.

In any case this is what has fixed it for me, for now:
new FileWriter(“${workspace}/MyFile.csv”)