zoukankan      html  css  js  c++  java
  • String 和 InputStream 互转方式

     /**
         * 利用BufferedReader实现Inputstream转换成String <功能详细描述>
         * 
         * @param in
         * @return String
         */
        
        public static String Inputstr2Str_Reader(InputStream in, String encode)
        {
            
            String str = "";
            try
            {
                if (encode == null || encode.equals(""))
                {
                    // 默认以utf-8形式
                    encode = "utf-8";
                }
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, encode));
                StringBuffer sb = new StringBuffer();
                
                while ((str = reader.readLine()) != null)
                {
                    sb.append(str).append("
    ");
                }
                return sb.toString();
            }
            catch (UnsupportedEncodingException e1)
            {
                e1.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            
            return str;
        }
        
        /**
         * 利用byte数组转换InputStream------->String <功能详细描述>
         * 
         * @param in
         * @return
         * @see [类、类#方法、类#成员]
         */
        
        public static String Inputstr2Str_byteArr(InputStream in, String encode)
        {
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[1024];
            int len = 0;
            try
            {
                if (encode == null || encode.equals(""))
                {
                    // 默认以utf-8形式
                    encode = "utf-8";
                }
                while ((len = in.read(b)) != -1)
                {
                    sb.append(new String(b, 0, len, encode));
                }
                return sb.toString();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return "";
            
        }
        
        /**
         * 利用ByteArrayOutputStream:Inputstream------------>String <功能详细描述>
         * 
         * @param in
         * @return
         * @see [类、类#方法、类#成员]
         */
        public static String Inputstr2Str_ByteArrayOutputStream(InputStream in,String encode)
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            try
            {
                if (encode == null || encode.equals(""))
                {
                    // 默认以utf-8形式
                    encode = "utf-8";
                }
                while ((len = in.read(b)) > 0)
                {
                    out.write(b, 0, len);
                }
                return out.toString(encode);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return "";
        }
        
        /**
         * 利用ByteArrayInputStream:String------------------>InputStream <功能详细描述>
         * 
         * @param inStr
         * @return
         * @see [类、类#方法、类#成员]
         */
        public static InputStream Str2Inputstr(String inStr)
        {
            try
            {
                // return new ByteArrayInputStream(inStr.getBytes());
                // return new ByteArrayInputStream(inStr.getBytes("UTF-8"));
                return new StringBufferInputStream(inStr);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
  • 相关阅读:
    oracle Constraint[相似 constraint使用方法总结 I]
    四个好看的CSS样式表格
    自己动手写操作系统--个人实践
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    海量数据存储
    windows的定时任务设置
    人脸识别算法初次了解
    Java替代C语言的可能性
    二叉排序树
    海量数据处理面试题集锦
  • 原文地址:https://www.cnblogs.com/mengzw/p/5112712.html
Copyright © 2011-2022 走看看