== Why is this an issue?

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 for ``++Files.notExists++``, ``++Files.isDirectory++`` and ``++Files.isRegularFile++`` from ``++java.nio.file++`` package.


*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is not 8.


=== Noncompliant code example

[source,java]
----
Path myPath;
if(java.nio.file.Files.exists(myPath)) {  // Noncompliant
 // do something
}
----


=== Compliant solution

[source,java]
----
Path myPath;
if(myPath.toFile().exists())) { 
 // do something
}
----


== Resources

* https://bugs.openjdk.java.net/browse/JDK-8153414[https://bugs.openjdk.java.net/browse/JDK-8153414]
* https://bugs.openjdk.java.net/browse/JDK-8154077[https://bugs.openjdk.java.net/browse/JDK-8154077]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Replace this with a call to the "toFile().exists()" method


=== Highlighting

``++Files.exists++``


'''
== Comments And Links
(visible only on this page)

=== on 22 Sep 2016, 20:12:28 Ann Campbell wrote:
https://bugs.openjdk.java.net/browse/JDK-8153414 

https://bugs.openjdk.java.net/browse/JDK-8154077

=== on 15 Nov 2019, 12:34:34 Tibor Blenessy wrote:
Removed from default profile because Java 8 is becoming quite old and using Files.exists is in general better approach when using Path API. See also \https://community.sonarsource.com/t/sonar-should-flag-calls-to-path-tofile/16664/2

endif::env-github,rspecator-view[]
