在Mac中解码使用base64编码的文件

时间:2018-07-19 03:08:33

标签: java macos unix decode encode

我有一个Mac图书中以base 64编码的文件-

base64 cwallet.sso > cwallet.base64.sso

如何使用Java解码此文件?

我尝试使用下面的代码,但是生成的文件几乎是原始“ cwallet.sso”文件的两倍。当我使用base64 -D cwallet.base64.sso > cwallet.original.sso进行编码时,得到的文件与cwallet.sso的文件大小相同。

Java代码

File file = new File("<path>/cwallet_newfile.sso");
        String contents = FileUtils.readFileToString(new File("<path>/cwallet_base64.sso"), "UTF-8");
        byte[] decodedBytes = Base64.getDecoder().decode(contents.trim());
        String decodedString = new String(decodedBytes);
        FileUtils.write(file,decodedString,"UTF-8");

1 个答案:

答案 0 :(得分:3)

请勿将(可能是)二进制文件转换为UTF-8 String;而是用FileUtils.writeByteArrayToFile(File, byte[])像这样写byte[]

byte[] decodedBytes = Base64.getDecoder().decode(contents.trim());
// String decodedString = new String(decodedBytes);
// FileUtils.write(file,decodedString,"UTF-8");
FileUtils.writeByteArrayToFile(file, decodedBytes);
相关问题