使用JDK/11,您可以使用writeBytes(byte b[])API,它最终会调用write(b, 0, b.length),正如answer by Josh中建议的那样
/**
* Writes the complete contents of the specified byte array
* to this {@code ByteArrayOutputStream}.
*
* @apiNote
* This method is equivalent to {@link #write(byte[],int,int)
* write(b, 0, b.length)}.
*
* @param b the data.
* @throws NullPointerException if {@code b} is {@code null}.
* @since 11
*/
public void writeBytes(byte b[]) {
write(b, 0, b.length);
}
示例代码将简单地转换为
byte[] bytes = new byte[100];
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
baos.writeBytes(bytes);
# 2 楼答案
无法显示ByteArrayOutputStream。我猜你想做的是
byte[] bytes = ...
String text = new String(bytes, "UTF-8"); // or some other encoding.
// display text.
# 1 楼答案
使用JDK/11,您可以使用
writeBytes(byte b[])
API,它最终会调用write(b, 0, b.length)
,正如answer by Josh中建议的那样示例代码将简单地转换为
# 2 楼答案
无法显示ByteArrayOutputStream。我猜你想做的是
您可以让ByteArrayOutputStream执行类似的操作,但这不是显而易见的、有效的或最佳实践(因为您无法控制所使用的编码)
# 3 楼答案
方法说明: