Convert InputStream to BufferedReader in Java

Note The BufferedReader reads characters; while the InputStream is a stream of bytes. The BufferedReader can’t read the InputStream directly; So, we need to use an adapter like InputStreamReader to convert bytes to characters format. For example: // BufferedReader -> InputStreamReader -> InputStream BufferedReader br = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets.UTF_8)); 1. Reads a file …

Read more

How to convert InputStream to String in Java

This article shows a few ways to convert an java.io.InputStream to a String. Table of contents 1. ByteArrayOutputStream 2. InputStream#readAllBytes (Java 9) 3. InputStreamReader + StringBuilder 4. InputStreamReader + BufferedReader (modified line breaks) 5. Java 8 BufferedReader#lines (modified line breaks) 6. Apache Commons IO 7. Download Source Code 8. References What are modified line breaks? …

Read more

How to convert String to InputStream in Java

In Java, we can use ByteArrayInputStream to convert a String to an InputStream. String str = "mkyong.com"; InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); Table of contents 1. ByteArrayInputStream 2. Apache Commons IO – IOUtils 3. Download Source Code 4. References 1. ByteArrayInputStream This example uses ByteArrayInputStream to convert a String to an InputStream and saves it …

Read more

How to convert InputStream to File in Java

Below are some Java examples of converting InputStream to a File. Plain Java – FileOutputStream Apache Commons IO – FileUtils.copyInputStreamToFile Java 7 – Files.copy Java 9 – InputStream.transferTo 1. Plain Java – FileOutputStream This example downloads the google.com HTML page and returns it as an InputStream. And we use FileOutputStream to copy the InputStream into …

Read more