Java ByteArrayOutputStream.toString()方法具有什么功能呢?
下文笔者讲述ByteArrayOutputStream.toString()方法的功能简介说明,如下所示
ByteArrayOutputStream.toString()方法的功能:
将ByteArrayOutputStream对象按照指定字符集转换为相应的字符串
ByteArrayOutputStream.toString()方法的语法:
public String toString()
public String toString(String charsetName)
public String toString(int hibyte)
参数说明
charsetName:指定字符集(utf-8,GB2312等)。
返回值说明
从缓冲区内容解码的字符串
例
public static void main(String args[]) throws Exception
{
InputStream in = null;
ByteArrayOutputStream out = null;
try
{
in = new FileInputStream(new File("D:\\java265.txt"));
out = new ByteArrayOutputStream();
byte[] bytes = new byte[1024 * 8];
int len = 0;
while((len = in .read(bytes)) != -1)
{
out.write(bytes, 0, len);
}
System.out.println(out.toString("GBK"));//使用GBK编码转换成字符串
}
catch(FileNotFoundException e)
{}
finally
{
try
{
out.close(); in .close();
}
catch(IOException e)
{}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


