以下代码
import java.util.Base64;
try (final InputStream fis = new FileInputStream(new File("foobar.png"))) {
final String str = Base64.getEncoder().encodeToString(fis.readAllBytes());
System.out.println("+++" + str + "+++"); // prints nothing!
System.out.println("+++" + str.length() + "+++"); // +++34500+++
System.out.println("+++" + str.isBlank() + "+++"); // +++false+++
try (final OutputStream os = new FileOutputStream(new File("foobar.txt"))) {
os.write(str.getBytes()); // empty file!
}
final String str2 = "foobar";
try (final OutputStream os2 = new FileOutputStream(new File("foobar2.txt"))) {
os2.write(Base64.getEncoder().encodeToString(str2.getBytes()); // This works!
}
}不会打印任何东西,这是System.out.println的第一次调用。怎么了?此外,当我用base64编码的字符串编写文件时,文件是空的。
当我将所有读取的字节写回没有base64编码的文件系统时,这两个文件是相等的。所以,没关系。但是编码有什么问题呢?
foobar.png在文件系统上有26 KiB。
采用JDK 11.0.3+7
月食: 2019-03 (4.11.0)
解决方案
多亏了罗伯特。这显然只是我的Eclipse版本中的显示问题。
https://stackoverflow.com/questions/61341554
复制相似问题