By kswaughs | Monday, June 8, 2015

How to read file into String in Java

Use Apache commons FileUtils class.

This is one of the best ways to read the contents of a file and store into String object.
It is a two lines statement code and it makes your application code more readable and better understandable.
Two static methods of FileUtils class are used and hence no object creation is required.

File file = FileUtils.getFile("path_to_file");
String content= FileUtils.readFileToString(file);

To use this class in your applications, You have to download the commons-io jar of Apache Commons Group.

Download the jar from below link.

To download the jar from Maven, Use below configuration in your project pom.xml file.


Recommend this on

No comments:

Post a Comment