The checkSameContent() method takes two file paths as input and reads their contents using the readAllBytes() method from the java.nio.file.Files class. It then uses the equals() method from the java.util.Arrays class to compare the byte arrays. If the contents are the same, the method returns true; otherwise, it returns false. In the main() method, we call the checkSameContent() method and print the result.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
public class Main {
public static boolean checkSameContent(String file1, String file2) throws IOException {
byte[] content1 = Files.readAllBytes(Paths.get(file1));
byte[] content2 = Files.readAllBytes(Paths.get(file2));
return java.util.Arrays.equals(content1, content2);
}
public static void main(String[] args) throws IOException {
if (checkSameContent("file1.txt", "file2.txt")) {
System.out.println("The files have the same content");
} else {
System.out.println("The files have different content");
}
}
}