Apache Commons IO to read text content from Reader

Tags: Apache Commons Apache Commons IO

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 Reader.

package simplesolution.dev;

import org.apache.commons.io.IOUtils;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class IOUtilToStringFromReaderExample {

    public static void main(String... args) {
        String fileName = "D:\\data\\sample.txt";
        try(Reader reader = new FileReader(fileName)) {
            String fileContent = IOUtils.toString(reader);
            System.out.println(fileContent);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Happy Coding 😊