Files
openide/java/java-impl/resources/inspectionDescriptions/BulkFileAttributesRead.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

32 lines
1.4 KiB
HTML

<html>
<body>
Reports multiple sequential <code>java.io.File</code> attribute checks, such as:
<ul>
<li><code>isDirectory()</code></li>
<li><code>isFile()</code></li>
<li><code>lastModified()</code></li>
<li><code>length()</code></li>
</ul>
Such calls can be replaced with a bulk <code>Files.readAttributes()</code> call.
This is usually more performant than multiple separate attribute checks.
<p>Example:</p>
<pre><code>
boolean isNewFile(File file, long lastModified) throws IOException {
return file.isFile() && file.lastModified() > lastModified;
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
boolean isNewFile(File file, long lastModified) throws IOException {
var fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
return fileAttributes.isRegularFile() && fileAttributes.lastModifiedTime().toMillis() > lastModified;
}
</code></pre>
<!-- tooltip end -->
<p>This inspection does not show a warning if <code>IOException</code> is not handled in the current context, but the quick-fix is still available.</p>
<p>Note that the replacements are usually not completely equivalent and should be applied with care. In particular, the behavior could differ if
the file does not exist at all.</p>
<p>This inspection only reports if the language level of the project or module is 7 or higher.</p>
<p><small>New in 2022.1</small></p>
</body>
</html>