一、OutputStream寫入String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//向OutPutStream中寫入,如 message.writeTo(baos);
String str = baos.toString();
二、字元串轉inputStream
String string;
//
InputStream is = new ByteArrayInputStream(string.getBytes());
三、.InputStream轉字元串
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = is.read()) != -1) {
baos.write(i);
}
String str = baos.toString();
System.out.println(str);
四、String寫入OutputStream
OutputStream os = System.out;
os.write(string.getBytes());
五、OutputStream 與 InputStream 互轉
public InputStream inputStreamToOutPut(OutputStream os){
InputStream is = new ByteArrayInputStream(os.toString().getBytes());
return is;
}
六、InputStream to byte[]
public static final byte[] input2byte(InputStream inStream)
throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
七、byte[] to InputStream
public static InputStream byteToInputStream(byte[] buf){
InputStream sbs = new ByteArrayInputStream(buf);
return sbs;
}