zoukankan      html  css  js  c++  java
  • InputStream和OutputStream与String之间的转换

    //1.字符串转inputstream
            String str="aaaaa";
            InputStream in = new ByteArrayInputStream(str.getBytes());
            
            //2.inputstream转字符串
            String result = readFromInputStream(inputStream);//调用处
            //将输入流InputStream变为String
                public String readFromInputStream(InputStream in) throws IOException {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while ((len = in.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    baos.close();
                    in.close();
                    
                    byte[] lens = baos.toByteArray();
                    String result = new String(lens,"UTF-8");//内容乱码处理
                    
                    return result;
                
                }
            //3.String写入OutputStream中
            OutputStream out = System.out;  
            out.write(str.getBytes()); 
            
            //4.outputStream转string
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            //向OutPutStream中写入,如 message.writeTo(baos); 
            baos.write(str.getBytes());
            String str1= baos.toString();  
  • 相关阅读:
    [大山中学模拟赛] 2016.9.17
    [DP优化方法之斜率DP]
    Gengxin讲STL系列——String
    小班讲课之动态规划基础背包问题
    ubuntu安装体验
    小班出题之字符串基础检测
    G
    B
    小项目--反eclass
    树--天平问题
  • 原文地址:https://www.cnblogs.com/liun1994/p/4004252.html
Copyright © 2011-2022 走看看