Java – simple file comparison

I put together a very simplistic way to compare files. The code is not as fancy as diff or other tools, but it gets the job done.

[sourcecode language=”java”]

/**
Copyright 2012 James G. Willmore

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net.ljcomputing.diff;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
* A simple way to compare two files. Very basic, very raw.
*
* @author James G. Willmore
*/
public class SimpleDiff
{

/** The original (or source) file. */
private File originalFile;

/** The copy (or target) file. */
private File copyFile;

/** The original file input stream. */
private BufferedInputStream obis;

/** The copy file input stream. */
private BufferedInputStream cbis;

// ——————————————————————————–

/**
* Constructor.
*
* @param original the original (source) file
* @param copy the copy (target) file
*/
public SimpleDiff(String original, String copy)
{
this.originalFile = new File(original);
this.copyFile = new File(copy);
}

// ——————————————————————————–

/**
* Verify both files exist and are readable.
*
* @throws Exception
*/
private void precheck() throws Exception
{
if(!originalFile.exists())
throw new Exception("Original file does not exist.");

if(!copyFile.exists())
throw new Exception("Copy file does not exist.");

if(!originalFile.canRead())
throw new Exception("Original file is not readable.");

if(!copyFile.canRead())
throw new Exception("Copy file is not readable.");
}

// ——————————————————————————–

/**
* Open both the original and copy file input streams.
*
* @throws Exception
*/
private void openStreams() throws Exception
{
obis = new BufferedInputStream(new FileInputStream(originalFile));
cbis = new BufferedInputStream(new FileInputStream(copyFile));
}

// ——————————————————————————–

/**
* Close both the original and copy file input streams.
*
* @throws Exception the exception
*/
private void closeStreams() throws Exception
{
if(obis != null) obis.close();
if(cbis != null) cbis.close();
}

// ——————————————————————————–

/**
* Compare both files.
*
* @return true, if both files match byte for byte
* @throws Exception
*/
public boolean compareFiles() throws Exception
{
precheck();
openStreams();

boolean notDone = true;
boolean equal = true;

int o = -1;
int c = -1;

while(notDone)
{
o = obis.read();
c = cbis.read();

if(o == -1 || c == -1)
notDone = false;

if(o != c)
equal = false;
}

closeStreams();

return equal;
}

// ——————————————————————————–

/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args)
{
SimpleDiff diff = new SimpleDiff("original.txt", "copy.txt");

try
{
System.out.println("Are files equal?: " + diff.compareFiles());
}
catch(Exception e)
{
e.printStackTrace();
}
}

// ——————————————————————————–

}
[/sourcecode]