将换行符附加到 Base64 编码的字符串。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
encodeBase64String(byte[])
您调用的方法是一个静态
方法,因此方法调用未使用您创建的Base64
实例。您应该使用
encodeToString(byte[])
方法,这是一个实例方法。The
encodeBase64String(byte[])
method you are calling is astatic
method, so theBase64
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.