package com.common.learn;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
/**
* @author 30378
*
* java.io.ByteArrayOutputStream:toString(Charset)
* 重载toString()方法,通过使用指定的字符编码字节,
* 在缓冲区的内容转换为字符串代码
* **/
public class Jdk10Day03 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "我是中国人";
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("utf-8"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int c = 0;
while((c = bis.read()) != -1) {
bos.write(c);
}
System.out.println(bos.toString("utf-8"));
System.out.println(bos.toString("gbk"));
}
}
转载于:https://my.oschina.net/fendouan/blog/2445513