0

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:

Compare between source (right) and copy (left)

Can anybody tell me where those come from and how I can get my correct file content?

4
  • 2
    xslx is binary file. You need to treat it like that. Commented Mar 25 at 9:28
  • 1
    If you just want to copy a file 1:1 you don't need to care about encoding. You can copy bytes directly from SmbFileInputStream to FileOutputStream. stackoverflow.com/questions/43157/… Commented Mar 25 at 10:20
  • Thanks a lot, IOUtils.copy(new SmbFileInputStream(sFile),Files.newOutputStream(file.toPath())) did the job Commented Mar 25 at 11:24
  • Is there something wrong with using e.g. sh "cp $sFile $file" if all you need is a copy? Commented Mar 25 at 15:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.