System.out.println显示空白行

时间:2018-10-16 06:12:19

标签: string java-8 base64

为什么下面的'System.out.println'(fileEncodedString)在调试时在值存在的地方显示空白行,而在对值进行解码和打印时却得到输出。

byte[] fileContent = readFileToByteArray(filePath);
String fileEncodedString = Base64.getEncoder().encodeToString(fileContent);
byte[] fileDecodedString = Base64.getDecoder().decode(fileEncodedString);
System.out.println("Base64 Decoded File (Basic): " + new String(fileDecodedString, "utf-8"));

private static byte[] readFileToByteArray(String filePath) {
    FileInputStream fileInputStream = null;
    byte[] bytesArray = null;
    try {
        File file = new File(filePath);
        bytesArray = new byte[(int) file.length()];
        // read file into bytes[]
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bytesArray);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    return bytesArray;
}

1 个答案:

答案 0 :(得分:1)

尝试对readFileToByteArray使用以下方法并传递正确的文件路径:

static byte[] readFileToByteArray(String filePath) throws IOException {
        File file = new File(filePath);
        return Files.readAllBytes(file.toPath());

}

工作正常。