1.(StringBuffer+InputStreamReader+BufferedReader ),不推荐使用,BufferedReader在readLine() 时会在读取到换行符时直接返回,然后读取下一行,会丢失换行符(what fk is that?)。
public String inputStreamString (InputStream in) throws IOException {
String tempLine="";
StringBuffer resultBuffer = new StringBuffer();
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
return resultBuffer.toString();
}
2.(ByteArrayOutputStream)推荐使用
public String intputStreamString2(InputStream inputStream){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
try {
while((i=inputStream.read())!=-1){
baos.write(i);
}
return baos.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
网友评论