What to import to use IOUtils.toString()?

user2380101 picture user2380101 · May 14, 2013 · Viewed 62.5k times · Source

I am trying to use IOUtils.toString() to read from a file. However, I am getting an error saying "IOUtils cannot be resolved."

What am I supposed to be importing to allow me to use this function?

String everything = IOUtils.toString(inputStream);

Thanks

Answer

Fryta picture Fryta · Jun 7, 2016

import org.apache.commons.io.IOUtils;

If you still can't import add to pom.xml:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

or for direct jar/gradle etc visit: http://mvnrepository.com/artifact/commons-io/commons-io/2.5

Also since version 2.5 of commons-io method IOUtils.toString(inputStream) has been deprecated. You should use method with Encoding i.e.

IOUtils.toString(is, "UTF-8");