|
Academic Work Personal
|
Java /
InputStream to String; String to InputStream
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Convert
{
/**
* Convert an InputStream to a String.
*
* @param str the InputStream to be transformed
* @return the String format of the InputStream
*/
public static String inputStreamToString(InputStream str)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(str));
StringBuilder sb = new StringBuilder();
try {
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
str.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* Convert a String to an InputStream.
*
* @param str
* @return
*/
public static InputStream stringToInputStream(String str)
{
InputStream is = null;
try
{
is = new ByteArrayInputStream(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex)
{
System.err.println("Error while converting String to InputStream: "
+ ex.getMessage());
}
return is;
}
} // class Convert
|
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |