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
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
Guava. Read file to List of Strings
Github Gists:
- How to read file to List of Strings with IOUtils
- How to read file to List of Strings with Google Guava
You may also find these posts interesting: