43

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

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
user2380101
  • 431
  • 1
  • 4
  • 4

6 Answers6

55

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");
Pang
  • 9,564
  • 146
  • 81
  • 122
Fryta
  • 593
  • 4
  • 6
14

import org.apache.commons.io.IOUtils;

SMR
  • 6,628
  • 2
  • 35
  • 56
kracejic
  • 723
  • 1
  • 6
  • 17
10

Fryta's answer outline how to actually use IOUtils and snj's answer is good for files.

If you're on java 9 or later and you have an input stream to read you can use InputStream#readAllBytes(). Just create a string from there and don't forget to specify charset.

String s = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
thoredge
  • 12,237
  • 1
  • 40
  • 55
  • This is the only answer that worked for me.. running Java v17. – Charles Jul 04 '22 at 10:27
  • 2
    When the `inputStream` originates from a `Path` (or a `File` which you can [easily convert to a `Path`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html#toPath())), you can use [`String s = Files.readString(path);`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path)) in the first place. Otherwise, this `readAllBytes()` based solution is the most pragmatic approach. – Holger Aug 25 '22 at 16:27
3

Alternatively you can try following way. It worked for me for reading public key for resource server

        final Resource resource = new ClassPathResource("public.key");
        String publicKey = null;
        try {
            publicKey = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
Sanjay Sharma
  • 3,687
  • 2
  • 22
  • 38
1

Here is code for How to convert InputStream to String in Java using Apache IOUtils

Reference : https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html

FileInputStream fis = new FileInputStream(FILE_LOCATION);
String StringFromInputStream = IOUtils.toString(fis, "UTF-8");
System.out.println(StringFromInputStream);

Let me know if you need more help.

Himanshu Arora
  • 688
  • 1
  • 9
  • 20
0

The following import statement would be required:

import org.apache.commons.io.IOUtils;

If the error "The import org.apache.commons.io cannot be resolved" shows up, add the commons-io-2.6.jar to the project Java buildpath and/or to the project lib folder.

Note: The suggestion to import "import org.apache.commons.io.IOUtils;" usually doesn't appear in Eclipse IDE at least in the context of this question.

Anish Narayan
  • 72
  • 2
  • 8