Read file contents with Apache Commons IO library in Java
This post will discuss how to read the contents of a file using the Apache Commons IO library in Java.
Several third-party libraries provide utility methods for working with files. If you prefer the Apache Commons IO library, it’s FileUtils class has several file manipulation utilities.
1. Using FileUtils.readLines(File, Charset) method
To read the contents of a file line by line into a list of strings, we can use the readLines() method from Apache Commons IO FileUtils class. It accepts the file to read and the character encoding to convert Bytes from the file into characters. It returns the list of strings representing each line in the file and throws IOException in case of an I/O error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; class Main { public static void main(String[] args) { File file = new File("demo.txt"); try { List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
2. Using FileUtils.readFileToString(File, Charset) method
Apache Commons IO FileUtils class provides the readFileToString() method that allows you to read the contents of any file into a string using the specified charset for converting Bytes from the file into characters. It throws IOException in case of an I/O error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("demo.txt"); String content = null; try { content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
3. Using IOUtils.toString(FileInputStream, Charset) method
Alternatively, we can use IOUtils class from Apache Commons IO library that has the toString() method. It takes an InputStream and renders its contents as string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("demo.txt"); String fileContents; try (FileInputStream inputStream = new FileInputStream(file)) { fileContents = IOUtils.toString(inputStream, StandardCharsets.UTF_8); System.out.println(fileContents); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading file contents with Apache Commons IO library in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)