Java.io.ByteArrayOutputStream.toString() 方法
描述
java.io.ByteArrayOutputStream.toString() 方法使用平台的默认字符集转换流的内容。 格式错误的输入和不可映射的字符序列被平台默认字符集的默认替换字符串替换。
声明
以下是 java.io.ByteArrayOutputStream.toString() 方法的声明 −
public String toString()
参数
NA
返回值
此方法返回从缓冲区内容解码的字符串。
异常
NA
示例
下面的例子展示了 java.io.ByteArrayOutputStream.toString() 方法的使用。
package com.tutorialspoint;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamDemo {
public static void main(String[] args) throws IOException {
String str = "";
byte[] bs = {65, 66, 67, 68, 69};
ByteArrayOutputStream baos = null;
try {
// create new ByteArrayOutputStream
baos = new ByteArrayOutputStream();
// write byte array to the output stream
baos.write(bs);
// converts buffers using default character set
str = baos.toString();
// print
System.out.println(str);
} catch(Exception e) {
// if I/O error occurs
e.printStackTrace();
} finally {
if(baos!=null)
baos.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
ABCDE

