Class FileCompare


  • public class FileCompare
    extends java.lang.Object
    FileCompare class provides a several methods, which compare two files or input streams. Most methods are static. It has a facility to incorporate a regex ignore-differences filter
    • Constructor Summary

      Constructors 
      Constructor Description
      FileCompare()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean compare​(java.io.File file1, java.io.File file2)
      compares two files and return true if the files have the same content.
      static boolean compare​(java.io.InputStream in1, java.io.InputStream in2)  
      static boolean compare​(java.lang.String filename1, java.io.InputStream in)
      compares two files and return true if the files have the same content.
      static boolean compare​(java.lang.String filename1, java.lang.String filename2)
      compares two files and return true if the files have the same content.
      static boolean compareStrings​(java.lang.String in1, java.lang.String in2)
      Compare 2 strings, ignoring whitespace characters
      static boolean compareStringsWithFilter​(java.lang.String s1, java.lang.String s2, java.util.regex.Pattern pattern)
      Compare 2 strings, showing where they differ in output to system.out, after doing filtering: normalize cr nl to nl normalize <xmltag:> </xmltag> to <xmltag/> normalize by applying supplied Pattern and deleting anything it matches normalize by converting all 2 or more spaces/tabs to just 1 space
      static boolean compareStringsWithMsg​(java.lang.String s1, java.lang.String s2)
      Compare two strings, give message indicating where they miscompare, including approx 10 chars before and after the first miscompare, for context
      static boolean compareWithFilter​(java.lang.String filename1, java.lang.String filename2, java.util.regex.Pattern pattern)
      Compares two files and returns true, if both have the same content, after filtering using the supplied Pattern.
      static boolean compareXML​(java.lang.String filename1, java.lang.String filename2)
      Compares two XML files and returns true, if both have the same content.
      static java.lang.String file2String​(java.io.File file)
      Read the contents of a file into a string, using the default platform encoding.
      static java.lang.String reader2String​(java.io.Reader reader, int bufSize)
      Read a bufferedReader into a string, using the default platform encoding.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileCompare

        public FileCompare()
    • Method Detail

      • compare

        public static boolean compare​(java.lang.String filename1,
                                      java.lang.String filename2)
                               throws java.io.IOException
        compares two files and return true if the files have the same content.
        Parameters:
        filename1 - filename of the first file
        filename2 - filename of the second file
        Returns:
        - true if the files have the same content
        Throws:
        java.io.IOException - -
      • compare

        public static boolean compare​(java.io.File file1,
                                      java.io.File file2)
                               throws java.io.IOException
        compares two files and return true if the files have the same content.
        Parameters:
        file1 - first file
        file2 - second file
        Returns:
        - true if the files have the same content
        Throws:
        java.io.IOException - -
      • compare

        public static boolean compare​(java.lang.String filename1,
                                      java.io.InputStream in)
                               throws java.io.IOException
        compares two files and return true if the files have the same content.
        Parameters:
        filename1 - filename of the first file
        in - an input Sream
        Returns:
        - true if the content is the same
        Throws:
        java.io.IOException - -
      • compare

        public static boolean compare​(java.io.InputStream in1,
                                      java.io.InputStream in2)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • compareStrings

        public static boolean compareStrings​(java.lang.String in1,
                                             java.lang.String in2)
        Compare 2 strings, ignoring whitespace characters
        Parameters:
        in1 - -
        in2 - -
        Returns:
        -
      • compareXML

        public static boolean compareXML​(java.lang.String filename1,
                                         java.lang.String filename2)
                                  throws java.io.IOException
        Compares two XML files and returns true, if both have the same content. Different notations for empty tags are considered equal.
        Parameters:
        filename1 - Filename of the first XML file.
        filename2 - Filename of the second XML file.
        Returns:
        -
        Throws:
        java.io.IOException - -
      • compareWithFilter

        public static boolean compareWithFilter​(java.lang.String filename1,
                                                java.lang.String filename2,
                                                java.util.regex.Pattern pattern)
                                         throws java.io.IOException
        Compares two files and returns true, if both have the same content, after filtering using the supplied Pattern. In addition, \r\n is normalized to \n, multiple spaces and tabs are normalized to a single space
        Parameters:
        filename1 - Filename of the first XML file.
        filename2 - Filename of the second XML file.
        pattern - an instance of Pattern which matches all substrings which should be filtered out of the match
        Returns:
        true, if both have the same content, after filtering using the supplied Pattern.
        Throws:
        java.io.IOException - -
      • compareStringsWithFilter

        public static boolean compareStringsWithFilter​(java.lang.String s1,
                                                       java.lang.String s2,
                                                       java.util.regex.Pattern pattern)
        Compare 2 strings, showing where they differ in output to system.out, after doing filtering: normalize cr nl to nl normalize <xmltag:> </xmltag> to <xmltag/> normalize by applying supplied Pattern and deleting anything it matches normalize by converting all 2 or more spaces/tabs to just 1 space
        Parameters:
        s1 - -
        s2 - -
        pattern - -
        Returns:
        -
      • compareStringsWithMsg

        public static boolean compareStringsWithMsg​(java.lang.String s1,
                                                    java.lang.String s2)
        Compare two strings, give message indicating where they miscompare, including approx 10 chars before and after the first miscompare, for context
        Parameters:
        s1 - first string to compare
        s2 - second string to compare
        Returns:
        true if strings have the same charactersS
      • file2String

        public static java.lang.String file2String​(java.io.File file)
                                            throws java.io.IOException
        Read the contents of a file into a string, using the default platform encoding.
        Parameters:
        file - The file to be read in.
        Returns:
        String The contents of the file.
        Throws:
        java.io.IOException - Various I/O errors. ' TODO: This is duplicated from org.apache.uima.internal.util.FileUtils in the uimaj-core package. We can't have a compile dependency on uimaj-core since that introduces a cycle. Not sure what the best way of handling this is.
      • reader2String

        public static java.lang.String reader2String​(java.io.Reader reader,
                                                     int bufSize)
                                              throws java.io.IOException
        Read a bufferedReader into a string, using the default platform encoding.
        Parameters:
        reader - to be read in
        bufSize - - size of stream, in bytes. Size in chars is ≤ size in bytes, because chars take 1 or more bytes to encode.
        Returns:
        String The contents of the stream.
        Throws:
        java.io.IOException - Various I/O errors. TODO: This is duplicated from org.apache.uima.internal.util.FileUtils in the uimaj-core package. We can't have a compile dependency on uimaj-core since that introduces a cycle. Not sure what the best way of handling this is.