Comparing identical files do not match

237 Views Asked by At

I am using Azure File Storage to upload files into Azure storage. While running an integration test, I download the file and compare it with the original it doesn't match. Sharing a sample code below

 public boolean downloadFile(String fileName, File expectedFile) throws Exception {
    File file = new File(fileName);
    //opens a connection to azure storage through helper method.
    this.azureConnection = openConnection(this.azureDir);

        CloudFile cloudFile = this.azureConnection.getFileReference(fileName);
        if (cloudFile.exists()) {
            cloudFile.downloadToFile(file.getAbsolutePath());
        } else {
            throw new Exception(fileName+" doesn't exist in Azure");
        }
    return FileUtils.contentEquals(expectedFile, file);
}

The above method always returns false. I also tried implementing a method using scanner to read line by line and write to a file. Still they do not match. Virtually I inspected both the files and compared spaced and new line, everything matches. Can someone please help resolve this issue.

//The below method is from org.apache.commons.io.FileUtils. I have simply copied and pasted it here for your reference.
 public static boolean contentEquals(File file1, File file2) throws IOException {
    boolean file1Exists = file1.exists();
    if (file1Exists != file2.exists()) {
        return false;
    }

    if (!file1Exists) {
        // two not existing files are equal
        return true;
    }

    if (file1.isDirectory() || file2.isDirectory()) {
        // don't want to compare directory contents
        throw new IOException("Can't compare directories, only files");
    }

    if (file1.length() != file2.length()) {
        // lengths differ, cannot be equal
        return false;
    }

    if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
        // same file
        return true;
    }

    InputStream input1 = null;
    InputStream input2 = null;
    try {
        input1 = new FileInputStream(file1);
        input2 = new FileInputStream(file2);
        return IOUtils.contentEquals(input1, input2);

    } finally {
        IOUtils.closeQuietly(input1);
        IOUtils.closeQuietly(input2);
    }
}
0

There are 0 best solutions below