From SonarLint, if you already have the path, use path.toFile().exists()
instead of Files.exists
for better performance.
The
Files.exists
method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.
The same goes forFiles.notExists
,Files.isDirectory
andFiles.isRegularFile
.
Noncompliant Code Example:
Path myPath;
if(java.nio.Files.exists(myPath)) { // Noncompliant
// do something
}
Compliant Solution:
Path myPath;
if(myPath.toFile().exists())) {
// do something
}