1.String to InputStream
String str = "string-to-inputstream conversion";
InputStream In_nocode = new Bytearrayinputstream (Str.getbytes ());
InputStream In_withcode = new Bytearrayinputstream (str.getbytes ("UTF-8"));
2.InputStream to String
Here are a few methods.
Method 1:
Public String convertstreamtostring (InputStream is) {
BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
StringBuilder sb = new StringBuilder ();
String line = null;
try {
while (line = Reader.readline ()) = null) {
Sb.append (line + "/n");
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
return sb.tostring ();
}
Method 2:
public string inputstream2string (inputstream in) throws ioexception {
stringbuffer out = new stringbuffer ();
byte[] b = new byte[ 4096];
for (int n; (n = In.read (b)) != -1;) {
out.append (new String (b, 0, N));
}
return out.tostring ();
}
Method 3:
public static String inputstream2string (InputStream is) throws ioexception{
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
int i=-1;
while ((I=is.read ())!=-1) {
Baos.write (i);
}
return baos.tostring ();
}
String and InputStream convert each other