Java NullReader-class And Method Code Example
Here is an example of how to use the NullReader
class from the Apache Commons IO library in Java:
import java.io.IOException;
import org.apache.commons.io.input.NullReader;
class Example {
public static void main(String[] args) {
try (NullReader nr = new NullReader()) {
char[] buffer = new char[1024];
int charsRead = nr.read(buffer);
System.out.println("Chars read: " + charsRead); // -1
} catch (IOException e) {
// handle exception
}
}
}
This code creates a NullReader
which is a reader that discards any characters read from it and always returns -1 when read.
In this example, it read from the NullReader
using the read method, it returns -1, since it's an empty reader.
It can be used to test code that requires a Reader, or when you want to discard data from a Reader but don't want to modify the original code.
It is equivalent to the NullInputStream, but for character data.