In this post under Apache Commons IO, I will show with example how to read a file and ignore the contents that we have read.
In Apache Commons IO framework, we have static utility class “IOUtils”, which has a method “consume” which reads a file and ignore the contents that it has read.
Below is the complete main code for your reference
Main class
1 package ioutils;
2
3 import org.apache.commons.io.IOUtils;
4
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.IOException;
8
9 public class Example1 {
10 public static void main(String[] args) throws IOException {
11 File file = new File("Quotes.txt");
12 try(FileReader fileReader = new FileReader(file)) {
13 IOUtils.consume(fileReader);
14 }
15 }
16 }
In the above code, at line 11, I create a “File” instance.
At line 12, within try-with-resource block, I am creating an instance of “FileReader” using the “file” object as constructor argument.
Then at line 13, I call “IOUtils” class static method “consume” and pass the “FileReader” instance as a method argument.
That is all we have to do.
The method “consume” reads the file contents and ignores whatever it has read.
In this way, we can read a file and at the same time ignore whatever is read.