//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();
时间: 2024-10-24 01:18:28