How to convert/read an input stream into a string in java?

Java 8Object Oriented ProgrammingProgramming

<p>Java provides FileInputStream class for reading data from a file. This class provides <b>read()</b> method to read contents of a file character by character. Create a <b>StringBuffer</b> object and append character by character to it using the <b> append()</b> method. Finally, convert the contents of StringBuffer object to String using <b>toString()</b> method.</p> <h2>Example</h2> <pre class="prettyprint notranslate"> import; import; import; public class StreamToString { public static void main(String args[]) throws IOException { File file = new File("da"); FileInputStream fis = new FileInputStream(file); int size=fis.available(); StringBuffer buffer = new StringBuffer(); for(int i = 0; i&lt; size; i++) { buffer.append((char); } System.out.println(buffer.toString()); } } </pre> <h2>Output</h2> <pre class="result notranslate"> Hi how are you </pre>
Updated on 30-Jul-2019 22:30:20