FileDocCategorySizeDatePackage
FileCompare.javaAPI DocphoneME MR2 API (J2ME)3642Wed May 02 17:59:48 BST 2007util

FileCompare

public class FileCompare extends Object

Fields Summary
public static boolean
debug
Constructors Summary
Methods Summary
public static booleancmp(java.io.File a, java.io.File b)

    if ( !a.isFile() || !b.isFile()){
	if ( debug ){
	    debugPrint(a, b, "not both plain files");
	}
	return false; // NotAFile compares unequal.
    }
    if ( a.length() != b.length() ){
	if ( debug ){
	    debugPrint(a, b, "different lengths");
	}
	return false; // different lengthes cannot be the same.
    }
    try {
	FileInputStream astream = null;
	FileInputStream bstream = null;
	try {
	    astream = new FileInputStream( a );
	    bstream = new FileInputStream( b );
	    long flength = a.length(); // == b.length(), remember?
	    int bufsize = (int)Math.min( flength, 1024 );
	    byte abuf[] = new byte[ bufsize ];
	    byte bbuf[] = new byte[ bufsize ];
	    long n = 0;
	    while ( n < flength ){
		int naread = astream.read( abuf );
		int nbread = bstream.read( bbuf );
		if ( naread != nbread ) return false; // oops.
		for ( int i = 0; i < naread; i++ ){
		    if ( abuf[i] != bbuf[i] ){
			if ( debug ){
			    debugPrint(a, b, "differ at byte "+(n+i) );
			}
			return false;
		    }
		}
		n += naread;
	    }
	} finally {
	    if ( astream != null ) astream.close();
	    if ( bstream != null ) bstream.close();
	}
    } catch ( IOException e ){
	e.printStackTrace();
	return false;
    }
    if ( debug ){
	debugPrint(a, b, "are the same");
    }
    return true;
public static voidconditionalCopy(java.io.File fromFile, java.io.File toFile)

    if ( ! cmp( fromFile, toFile ) )
	cpy( fromFile, toFile );
public static booleancpy(java.io.File a, java.io.File b)

    try {
	FileInputStream astream  = null;
	FileOutputStream bstream = null;
	try {
	    astream = new FileInputStream( a );
	    bstream = new FileOutputStream( b );
	    long flength = a.length();
	    int bufsize = (int)Math.min( flength, 1024 );
	    byte buf[] = new byte[ bufsize ];
	    long n = 0;
	    while ( n < flength ){
		int naread = astream.read( buf );
		bstream.write( buf, 0, naread );
		n += naread;
	    }
	} finally {
	    if ( astream != null ) astream.close();
	    if ( bstream != null ) bstream.close();
	}
    } catch ( IOException e ){
	e.printStackTrace();
	return false;
    }
    return true;
private static voiddebugPrint(java.io.File a, java.io.File b, java.lang.String msg)


          
    System.err.print(a.getPath());
    System.err.print(", ");
    System.err.print(b.getPath());
    System.err.print(" ");
    System.err.println( msg );
public static voidmain(java.lang.String[] args)

    debug = true;
    conditionalCopy( new File( args[0] ), new File( args[1] ) );