How to convert String in InputStream and backFrom WikiJavabuy this book
This article shows how to convert a
Convert from a String to an InputStreamByteArrayInputStream inputStream = new ByteArrayInputStream(original.getBytes()); Convert from an InputStream to a StringUnfortunately this is slightly more complex, but in the end nothing too critical. Basically The conversion from inputStream to String consists in reading the inputStream and saving it's content in a String. In this example I used a StringBuffer, to speed up the creation of the String. StringBuffer buffer = new StringBuffer(); byte[] b = new byte[4096]; try { for (int n; (n = inputStream.read(b)) != -1;) { buffer.append(new String(b, 0, n)); } } catch (IOException e) { e.printStackTrace(); } String result = buffer.toString();
|
