美文网首页
StringUtils的工具类

StringUtils的工具类

作者: wz9527 | 来源:发表于2017-05-30 19:50 被阅读0次

public class StringUtils {

    public static String inputStreamToString(InputStream inputStream) { 

        try { 
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

            byte[] buf = new byte[1024]; 

            int length = 0; 

            while ((length = inputStream.read(buf)) != -1) { 

                byteArrayOutputStream.write(buf, 0, length); 

            } 

            return new String(byteArrayOutputStream.toByteArray()); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return null; 
    } 

    public static String inputStraemToStringBuffer(InputStream inputStream) { 
        StringBuilder stringBuilder = new StringBuilder(); 
        try { 
            String line = null; 
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
            while ((line = bufferedReader.readLine()) != null) { 
                stringBuilder.append(line + "\n"); 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally { 
            return stringBuilder.toString(); 
        } 


    } 

}

相关文章

网友评论

      本文标题:StringUtils的工具类

      本文链接:https://www.haomeiwen.com/subject/hpwrfxtx.html