首页 > 代码库 > InputStream和OutputStream与String之间的转换
InputStream和OutputStream与String之间的转换
//1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getBytes()); //2.inputstream转字符串 String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readFromInputStream(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } baos.close(); in.close(); byte[] lens = baos.toByteArray(); String result = new String(lens,"UTF-8");//内容乱码处理 return result; } //3.String写入OutputStream中 OutputStream out = System.out; out.write(str.getBytes()); //4.outputStream转string ByteArrayOutputStream baos = new ByteArrayOutputStream(); //向OutPutStream中写入,如 message.writeTo(baos); baos.write(str.getBytes()); String str1= baos.toString();
InputStream和OutputStream与String之间的转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。