// 2012-08-16 johnpfeiffer requires FileSystem
// TODO: identification of one line difference in the middle (i.e. best guess line matching)
// TODO: buffering to provide large file size support
import java.io.IOException;
import java.util.ArrayList;
public class diff
{
public static final String CORRECTUSAGE = "java -jar diff filename1 filename2";
private ArrayList <String> firstFileAsArrayList;
private ArrayList <String> secondFileAsArrayList;
private StringBuilder result;
public static void main( String[] args )
{
if( args.length != 2 )
{
System.err.println( CORRECTUSAGE );
System.exit( 1 );
}
FileSystem fs = new FileSystem();
String firstFileName = args[0];
String secondFileName = args[1];
diff main = new diff();
main.result = new StringBuilder();
try{
main.firstFileAsArrayList = fs.listOfLinesFromFile( firstFileName );
main.secondFileAsArrayList = fs.listOfLinesFromFile( secondFileName );
long firstLineCount = main.firstFileAsArrayList.size();
long secondLineCount = main.secondFileAsArrayList.size();
if( firstLineCount != secondLineCount )
{
main.result.append( "Files do not match: " + firstLineCount + " != " + secondLineCount );
}
//TODO: currently exits on first non match
int counter = 0;
StringBuilder correspondingLine = new StringBuilder();
for( String line : main.firstFileAsArrayList )
{
= main.secondFileAsArrayList.get( counter );
if( !line.equals( correspondingLine ) )
{
main.result.append( "line number " + counter + " does not match" );
main.result.append( line );
main.result.append( line );
}
counter++;
}
}catch( IOException e )
{
main.result.append( "ERROR: " + e + " : " + CORRECTUSAGE );
}catch( IllegalArgumentException e )
{
main.result.append( "ERROR: " + e + " : " + CORRECTUSAGE );
}
}
private void cleanShutdown()
{
System.out.println( result.toString() );
}
} //end class