I am trying to copy an xslx file from a remote system within a jenkins groovy script. This xslx file is encoded with windows-1252, so I give this Charset to the FileReader and FileWriter:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("windows-1252"));
BufferedWriter bufferedWriter = new BufferedWriter(osw);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(sFile), Charset.forName("windows-1252")))) {
int byteRead;
while ((byteRead = reader.read()) != -1) {
bufferedWriter.write(byteRead);
}
bufferedWriter.flush();
}
The result xslx is encoded in windows-1252 and has nrly the same content, but there were additional questionmarks added into it:
Can anybody tell me where those come from and how I can get my correct file content?
Copying files does NOT require interpretation of their contents. All files are bags of bytes regardless if they are binary or text. Bytes do not require you interpret them (ie using Charsets).
This makes it easy for you to perform this using just InputStream and OutputStream.
file.withOutputStream { out ->
new SmbFileInputStream(sFile).withCloseable { smb ->
out << smb
}
}
This has been groovy'ed up so as to limit how much code you need to write, but it's essentially doing the exact same thing your code was doing, but only using InputStream and OutputStream. This also flushes and closes streams for you similar to try-resource statements.