//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();