zoukankan      html  css  js  c++  java
  • 流转换成字符串

    public class StreamUtil {
        /**
         * 流转换成字符串
         * @param is    流对象
         * @return        流转换成的字符串    返回null代表异常
         */
        public static String streamToString(InputStream is) {
            //1,在读取的过程中,将读取的内容存储值缓存中,然后一次性的转换成字符串返回
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //2,读流操作,读到没有为止(循环)
            byte[] buffer = new byte[1024];
            //3,记录读取内容的临时变量
            int temp = -1;
            try {
                while((temp = is.read(buffer))!=-1){
                    bos.write(buffer, 0, temp);
                }
                //返回读取数据
                return bos.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    is.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
  • 相关阅读:
    String类型操作命令及api使用
    回顾Redis基础
    kibana6 安装
    elasticsearch安装
    flink 异常
    Scala异常
    idea配置
    mysql error 1577解决
    Hbase与phoenix关联
    CS61b lab4打卡
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6203964.html
Copyright © 2011-2022 走看看