The Reader.read()
and the BufferedReader.readLine()
are used for reading data from a data source. The return value of
these methods is the data read from the data source, or null
when the end of the data source is reached. If the return value is ignored,
the data read from the source is thrown away and may indicate a bug.
This rule raises an issue when the return values of Reader.read()
and BufferedReader.readLine()
and their subclasses are
ignored or merely null-checked.
Noncompliant code example
public void doSomethingWithFile(String fileName) {
try(BufferedReader buffReader = new BufferedReader(new FileReader(fileName))) {
while (buffReader.readLine() != null) { // Noncompliant
// ...
}
} catch (IOException e) {
// ...
}
}
Compliant solution
public void doSomethingWithFile(String fileName) {
try(BufferedReader buffReader = new BufferedReader(new FileReader(fileName))) {
String line = null;
while ((line = buffReader.readLine()) != null) {
// ...
}
} catch (IOException e) {
// ...
}
}