Can someone explains to me why the 2nd println statement below is not printing anything? Even if I use the encodeToString() method instead of the encode() method, I still can't print the Base64 encoded string. The encoded string is not empty as I can verify using its length() method. I tried a different file, a binary one, and the problem is still there.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
public class Test {
public static void main(String... args) {
try {
byte[] content = Files.readAllBytes(Paths.get("D:\\config.properties"));
System.out.println(content.length); // correctly prints the length
byte[] encoded = Base64.getEncoder().encode(content);
System.out.println(new String(encoded)); // prints nothing
content = Base64.getDecoder().decode(encoded);
System.out.println(new String(content)); // prints original content
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
EDIT
This problem only exists when I run the program in Eclipse. When I run it from the command prompt, the Base64 encoded string can be printed out.