title: Jenkins Pipeline: How to write UTF-8 files with writeFile?
body: <p>I'm hoping this is not a bug and that I'm just doing something wrong. I have a Jenkins (v2.19.1) Pipeline job and in it's groovy script, I need to search and replace some text in an existing text file, on a Windows node.</p>
  
  <p>I've used fart.exe and powershell to do the search and replace, but I would really like to do this with just the groovy in Jenkins and eliminate dependency on fart/powershell/etc. and make this code more reusable on both linux and windows nodes.</p>
  
  <p>After much googling and trying various approaches, the closest I got was to use readFile and writeFile. However, I've not been able to get writeFile to create a UTF-8 file. It creates an ANSI file even when I specify UTF-8 (assuming I'm doing it correctly). </p>
  
  <p>Here's what I have so far...</p>
  
  <pre class="lang-java prettyprint-override"><code>def fileContents = readFile file: "test.txt", encoding: "UTF-8"
  fileContents = fileContents.replace("hello", "world")
  echo fileContents
  writeFile file: "test.txt", text: fileContents, encoding: "UTF-8"
  </code></pre>
  
  <p>I've confirmed with multiple text editors that the test.txt file is UTF-8 when I start, and ANSI after the writeFile line. I've tried all combinations of including/not-including the encoding property and "utf-8" vs "UTF-8". But in all cases, the file is written out as ANSI (as reported by both Notepad++ and VS Code). Also, a question mark (HEX 3F) is added as the very first character of the file. </p>
  
  <p>The echo line does not show the extra 3F character, so it seems the issue is in the writeFile line.</p>