最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Copy Excel file xslx encoding problems - Stack Overflow

programmeradmin4浏览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:

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

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?

Share Improve this question edited Mar 25 at 15:02 Kaffetrinken asked Mar 25 at 9:12 KaffetrinkenKaffetrinken 698 bronze badges 4
  • 2 xslx is binary file. You need to treat it like that. – talex 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/questions/43157/… – zett42 Commented Mar 25 at 10:20
  • Thanks a lot, IOUtils.copy(new SmbFileInputStream(sFile),Files.newOutputStream(file.toPath())) did the job – Kaffetrinken 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? – MaratC Commented Mar 25 at 15:03
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论