It has been proposed that a method named
isSameContents() be added to the Files class in JDK 11 via JDK-8202285 ["(fs) Add a method to Files for comparing file contents"]. Proposed by Joe Wang, this new method is "intended to be an extension to the existing isSameFile method since it stopped short of comparing the content to answer the query for whether two files are equal." JDK-8201276 also references this method and describes it as "a utility method that compares two files."
Regarding the usage of this new method, JDK-8202285's Description states:
Proposing a new
Filescurrently has a method called
isSameFilethat answers the query on whether or not two files are the same file. Since two files containing the same contents may also be viewed as the same, it is desirable to add a method that further compares the contents, that would make the "is same file" query complete.
The OpenJDK core-libs-dev mailing list discussion on this thread provides additional details on the background of, motivation for, and implementation of this new method. For example, there are messages on this thread that do the following:
- Ask if the method should instead be named
- Explain the choice of
isSameContent()to "stack it next to the existing isSameFile method since it's meant to be an extend to that method"
- Point out that the String class has already chosen singular "content" over plural "contents"
- Emphasize that the isSameFile(Path,Path) method "returns
truewithout checking if the file exists" if the
Paths being compared are equal
- States that one of the most common use cases of reading file content into a
Stringis for "a quick read of small files" and references Files.readString().
A particularly insightful message in this thread is a Rémi Forax message providing code demonstrating how to use the JDK 9-added InputStream.transfer(OutputStream) method, the JDK 10-added local variable type inference, and classes MessageDigest and DigestOutputStream to hash the contents of a file in six lines of Java code.
It's looking increasingly likely that JDK 11 will provide several new useful "utility" methods in addition to the JEPs and other more significant features that will be coming with JDK 11.