The material was borrowed from PhotoAC. Original image: Woman with PC
A file that is just a copy of the copy source file. Since there is no difference in the binary data, the comparison result is assumed to be True.
A slightly edited version of the source file after copying it. The small black dot on the right side of the face is the edited part. Since it was edited, the comparison result is assumed to be False.
Since it is a completely different file, the comparison result is assumed to be False.
Specify a file name that does not exist. Assumed to be caught by IOException.
Image files are stored in the "files" folder on the desktop.
Main.java
package samples.compare;
public class Main {
public static void main(String...strings) {
//Comparison source file
String woman_1 = "C:\\\\Users\\user\\Desktop\\files\\woman_1.jpg ";
//File to be compared
String woman_1_copy = "C:\\\\Users\\user\\Desktop\\files\\woman_1_copy.jpg ";
String woman_1_edit = "C:\\\\Users\\user\\Desktop\\files\\woman_1_edit.jpg ";
String woman_2 = "C:\\\\Users\\user\\Desktop\\files\\woman_2.jpg ";
String errer = "C:\\\\Users\\user\\Desktop\\files\\errer.jpg ";
//Instance generation
FileCompare fc_copy = new FileCompare(woman_1, woman_1_copy);
FileCompare fc_edit = new FileCompare(woman_1, woman_1_edit);
FileCompare fc_2 = new FileCompare(woman_1, woman_2);
FileCompare fc_errer = new FileCompare(woman_1, errer);
//Display comparison results
System.out.println("woman_1 compare to woman_1_copy : " + fc_copy.fileCompare() );
System.out.println("woman_1 compare to woman_1_edit : " + fc_edit.fileCompare() );
System.out.println("woman_1 compare to woman_2 : " + fc_2.fileCompare() );
System.out.println("woman_1 compare to errer : " + fc_errer.fileCompare() );
}
}
FileCompare.java
package samples.compare;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class FileCompare {
private String source;
private String destination;
public FileCompare(String source, String destination) {
this.source = source ;
this.destination = destination;
}
public boolean fileCompare() {
try {
return Arrays.equals(Files.readAllBytes(Paths.get(source)), Files.readAllBytes(Paths.get(destination)));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to read the file.");
}
return false;
}
}
As expected, everything except the copied files is Flase. Also, if you specify a file name that does not exist, an IOException was thrown.
woman_1 compare to woman_1_copy : true woman_1 compare to woman_1_edit : false woman_1 compare to woman_2 : false java.nio.file.NoSuchFileException: C:\Users\user\Desktop\files\errer.jpg at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source) at java.nio.file.Files.newByteChannel(Unknown Source) at java.nio.file.Files.newByteChannel(Unknown Source) at java.nio.file.Files.readAllBytes(Unknown Source) at samples.compare.FileCompare.fileCompare(FileCompare.java:19) at samples.compare.Main.main(Main.java:21) Failed to read the file. woman_1 compare to errer : false
Qiita How to determine if the file contents match (Java)
PhotoAC <a href="https://www.photo-ac.com/main/detail/3302344?title=PC%E3%82%92%E6%8C%81%E3%81%A4%E5%A5%B3 % E6% 80% A7 "rel =" noopener noreferrer "target =" _ blank "> Woman with PC <a href="https://www.photo-ac.com/main/detail/3411890?title=PC%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99 % E3% 82% 8B% E5% A5% B3% E6% 80% A7 "rel =" noopener noreferrer "target =" _ blank "> Woman operating a PC
Recommended Posts