字符串与字节流转换

public class StreamFromString {  

    static String src = "今天的天气真的不好";  
    public static void main(String[] args) throws IOException {  

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

你可能感兴趣的:(字符串与字节流转换,字符串与字节流转换)