I want to know How to convert InputStream to String in java. Please explain me with any example. Thanks in advanced.
I want to know How to convert InputStream to String in java. Please explain me with any example. Thanks in advanced.
1) Following illustration will show you how to convert InputStream to String:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws Exception
{
InputStream is = Main.class.getResourceAsStream("/data.txt");
System.out.println(convertStreamToString(is));
}
public static String convertStreamToString(InputStream is) throwsException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
}
Bookmarks