InputStream与String相互转换

来源:互联网 发布:qt编程是干什么的 编辑:程序博客网 时间:2024/05/16 17:57

//InputStream 转 String//方式一:public String inputStream2String0(InputStream is) throws IOException {String result = "";byte[] buf = new byte[1024];int len = 0;while((len = is.read(buf)) != -1){result += new String(buf, 0, len);}return result;}//方式二:public String inputStream2String1(InputStream is) throws IOException{ByteArrayOutputStream baos = new ByteArrayOutputStream();int   len = 0;while((len=is.read())!=-1){baos.write(len);}   return baos.toString();} //方式三:public String inputStream2String2(InputStream is) throws IOException{BufferedReader br = new BufferedReader(new InputStreamReader(is));StringBuffer sb = new StringBuffer();String line;while((line = br.readLine()) != null){sb.append(line);}   return sb.toString();}//String 转 InputStreampublic InputStream string2InputStream(String str){return new ByteArrayInputStream(str.getBytes());}




0 0
原创粉丝点击