We already know how to read file to String. What if we need to get it as a list of Strings. Let’s try to read the file that is placed in the “resources” folder as we did in the previous case. Guava and IOUtils are most used libraries for that. We’ll consider both.

The simpliest way to a read file to a list of Strings is using IOUtils from Apache Commons IO.

Apache Commons IO Maven dependency

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

IOUtils. Read file to List of Strings

That method reads the content of the file “fileName” and returns it as a list of strings.

Another popular approach to read file to a list of strings - Guava

Google Guava Maven dependency

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.0</version>
</dependency>

Guava. Read file to List of Strings

Github Gists:


You may also find these posts interesting: