将换行符附加到 Base64 编码的字符串。

发布于 2024-11-18 14:02:06 字数 563 浏览 5 评论 0原文

byte[] serObj = getBytesFromFile(file);
final byte[] CLRF = { '\r', '\n' };
Base64 encoded = new Base64 (72,CLRF);
System.out.println(encoded.encodeBase64String(serObj));

我在格式化输出时遇到问题,该输出当前显示为单行,而不是根据构造函数中的参数。它应该是一行 72 个字符,后面是 CLRF 和下一行。有人可以指出代码有什么问题吗?另外,如何在字符串中手动追加/添加换行符?我尝试使用字符计数器,但我不知道如何在计数器到达第 72 个字符后添加 \n 。

public static int count(Reader in) throws IOException {
char[] buffer = new char[4096];
int count = 0;
int len;
while((len = in.read(buffer)) != -1) {
    count += len;
}
    return count;
}
byte[] serObj = getBytesFromFile(file);
final byte[] CLRF = { '\r', '\n' };
Base64 encoded = new Base64 (72,CLRF);
System.out.println(encoded.encodeBase64String(serObj));

I am having problems formatting the output, which currently displays as a single line, and not according to the args in constructor. It is supposed to be a line with 72 chars and followed by CLRF and the next line. Can someone point out what's wrong with the code? Also, how could I manually append/add a newline char within a String? I tried using a char counter, but I am stuck on how to add the \n once the counter reaches the 72nd char.

public static int count(Reader in) throws IOException {
char[] buffer = new char[4096];
int count = 0;
int len;
while((len = in.read(buffer)) != -1) {
    count += len;
}
    return count;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2024-11-25 14:02:06

encodeBase64String(byte[]) 您调用的方法是一个静态方法,因此方法调用未使用您创建的 Base64 实例。

您应该使用 encodeToString(byte[]) 方法,这是一个实例方法。

The encodeBase64String(byte[]) method you are calling is a static method, so the Base64 instance that you created is not being used by the method call.

You should be using the encodeToString(byte[]) method, which is an instance method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文