Reading files using Guava’s Files class in Java
This post will discuss how to read the contents of a file using Guava library in Java.
There are several external libraries that provide utility methods for working with files in Java. With Guava library, we can use Files utility class which provides various methods for working with files and directories in Java. The Files class offers several features:
- Reading and writing files as strings, bytes, or lines
- Copying, moving, deleting, creating files, and creating directories
- Comparing the contents of two files
- Getting file attributes such as size, filename, extension, and checksum
- Creating temporary files and directories
To read contents of a file using the Files class by Guava library, we can use one of the following methods:
1. Using Files.readLines(File, Charset) method
We can use the readLines() method of the Files class that reads all the lines from a file into a mutable List. It accepts two parameters: the file to read from and the charset used to decode the input stream. It throws an IOException if an I/O error occurs. Here is an example of using this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.io.Files; 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 = Files.readLines(file, StandardCharsets.UTF_8); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
Note that Files.readLines(File, Charset) method is convenient to read all lines in a single operation but does not include line-termination characters. We can easily handle this by joining each of the elements of the list with a line separator, 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 com.google.common.io.Files; 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"); String content = null; try { List<String> lines = Files.readLines(file, StandardCharsets.UTF_8); content = String.join(System.lineSeparator(), lines); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. Using Files.asCharSource(File, Charset) method
Another plausible way of reading files in Guava is using the Files.asCharSource() method. It returns a new CharSource for reading character data from the given file using the given character set. Then we can call the CharSource’s read() method, which reads the contents of this source as a string. This method returns an IOException if an I/O error occurs in the process of reading from this source. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.io.Files; 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 = Files.asCharSource(file, StandardCharsets.UTF_8).read(); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
To read the contents of the CharSource as a list instead of string, we can use the readLines() method, which returns an ImmutableList instance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.io.Files; 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"); List<String> lines; try { lines = Files.asCharSource(file, StandardCharsets.UTF_8).readLines(); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading the contents of a file with the Guava 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 :)