字符串和字节流相互转换

public static void main(String[] args) throws IOException {
    String a="babbabababbababababab";
    byte[] buff = new byte[1024];
    //从字符串获取字节写入流
    InputStream is = new ByteArrayInputStream(a.getBytes("utf-8"));
    System.out.println(is);
    int len = -1;
    while(-1 != (len = is.read(buff))) {
        //将字节数组转换为字符串
        String res = new String(buff, 0, len);
        System.out.println(res);
    }
}

你可能感兴趣的:(摘抄)