java字符串转换为指定长度的数组_Java ByteArrayOutputStream.toString()指定字符集转换为字符串...

首页 > 基础教程 > IO流 > OutputStream类

Java ByteArrayOutputStream.toString()指定字符集转换为字符串

定义

public String toString()

public String toString(String charsetName)

public String toString(int hibyte)

参数

charsetName:指定字符集(utf-8,GB2312等)。

返回

从缓冲区内容解码的字符串。

异常

UnsupportedEncodingException :不支持指定的字符集

实例

public static void main(String args[]) throws Exception

{

InputStream in = null;

ByteArrayOutputStream out = null;

try

{

in = new FileInputStream(new File("c:\\51gjie.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)

{}

}

}

ByteArrayOutputStream.toString通过使用指定的charsetName解码字节,将缓冲区的内容转换为字符串。 新String的长度是字符集的函数,因此可能不等于字节数组的长度。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值