logo

Apache Commons IO to read text content from InputStream

Java Code Examples for org.apache.commons.io.IOUtils.toString()

The example below to show you how to use Apache Commons IO libary’s IOUtils class to read text content from InputStream.

package simplesolution.dev;

import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtilToStringFromInputStreamExample {

    public static void main(String... args) {
        String fileName = "D:\\data\\sample.txt";
        try(InputStream inputStream = new FileInputStream(fileName)) {
            String value = IOUtils.toString(inputStream, "UTF-8");
            System.out.println(value);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Happy Coding 😊