我的做法是
(1)先比較兩個檔案内容的長度;
(2)在長度相同的情況下,再比較兩個檔案的md5值。

【create md5】按鈕用于生成source file的檔案内容長度和md5值。
運作主類:checksameapp
package com.hw.main;
import java.awt.borderlayout;
import java.awt.container;
import java.awt.dimension;
import java.awt.gridbagconstraints;
import java.awt.gridbaglayout;
import java.awt.insets;
import java.awt.toolkit;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.file;
import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jtextfield;
import com.common.util.systemutil;
import com.swing.dialog.dialogutil;
import com.swing.messagebox.guiutil23;
public class checksameapp extends jframe
{
private static final long serialversionuid = 1644076682819874235l;
private jtextfield sourcefiletf;
private jbutton browsersourcebtn;
private jtextfield targetfiletf;
private jbutton createmd5button = null;
private jbutton checkmd5btn = null;
private jbutton browsertargetbtn = null;
/***
* md5 of last file
*/
private string result = null;
private long size_of_file = 0;
private jbutton comparebtn = null;
private file srcfile = null;
private file targfile = null;
protected static string mesg_diff = "[failed:] they are different";
protected static string mesg_same = "[successfully:] they are same completely";
public static void main(string[] args)
{
checksameapp app = new checksameapp();
app.launchframe();
}
public void launchframe()
this.settitle("compare two files by md5");
dimension screensize = toolkit.getdefaulttoolkit().getscreensize();
this.setsize(700, 200);
dimension framesize = this.getsize();
int x = (int) screensize.getwidth() / 2 - (int) framesize.getwidth()
/ 2;
int y = (int) screensize.getheight() / 2 - (int) framesize.getheight()
this.setlocation(x, y);
container c = this.getcontentpane();
layout(c);
this.setvisible(true);
this.setdefaultcloseoperation(jframe.exit_on_close);
* setting menu
public void layout(container c)
// setmenu2();
jpanel mainpane = new jpanel();
gridbaglayout gridbaglayout = new gridbaglayout();
gridbaglayout.columnwidths = new int[]
{ 20, 80/*between source file and text field*/, 300, 60, 0 };
gridbaglayout.rowheights = new int[]
{ 17, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0 };
gridbaglayout.columnweights = new double[]
{ 0.0, 0.0, 1.0, 1.0, double.min_value };
gridbaglayout.rowweights = new double[]
{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
double.min_value };
mainpane.setlayout(gridbaglayout);
jlabel ftpserverlb = new jlabel("source file");
gridbagconstraints gbc_setconnectionslabel = new gridbagconstraints();
gbc_setconnectionslabel.anchor = gridbagconstraints.west;
gbc_setconnectionslabel.insets = new insets(0, 0, 5, 5);
gbc_setconnectionslabel.gridx = 1;
gbc_setconnectionslabel.gridy = 1;
mainpane.add(ftpserverlb, gbc_setconnectionslabel);
// dialog.add(ftpservertf);
sourcefiletf = new jtextfield();
if (!systemutil.iswindows)
{
sourcefiletf.settext("/home/whuang2/bin/ab.txt");
}
else
sourcefiletf.settext("");
gridbagconstraints gbc_connectionstf = new gridbagconstraints();
gbc_connectionstf.fill = gridbagconstraints.horizontal;
gbc_connectionstf.insets = new insets(0, 0, 5, 5);
gbc_connectionstf.gridx = 2;
gbc_connectionstf.gridy = 1;
mainpane.add(sourcefiletf, gbc_connectionstf);
browsersourcebtn = new jbutton("browser source");
gridbagconstraints gbc_browsersourcebtn = new gridbagconstraints();
gbc_browsersourcebtn.fill = gridbagconstraints.east;
gbc_browsersourcebtn.insets = new insets(0, 0, 5, 5);
gbc_browsersourcebtn.gridx = 3;
gbc_browsersourcebtn.gridy = 1;
mainpane.add(browsersourcebtn, gbc_browsersourcebtn);
browsersourcebtn.addactionlistener(new actionlistener()
@override
public void actionperformed(actionevent e)
{
// system.out.println("source");
boolean issuccess = dialogutil.browser3(sourcefiletf,
jfilechooser.files_only, checksameapp.this);
// if (issuccess)
// {
// targetfiletf.settext(systemutil.getparentdir(sourcefiletf
// .gettext()));
// }
}
});
jpanel buttonpane = new jpanel();
// buttonpane.setbackground(color.red);
gridbagconstraints gbc_buttonpane = new gridbagconstraints();
gbc_buttonpane.fill = gridbagconstraints.horizontal;
gbc_buttonpane.insets = new insets(0, 0, 5, 5);
gbc_buttonpane.gridx = 2;
gbc_buttonpane.gridy = 2;
mainpane.add(buttonpane, gbc_buttonpane);
createmd5button = new jbutton("create md5");
checkmd5btn = new jbutton("check");
checkmd5btn.setenabled(false);
buttonpane.add(createmd5button);
buttonpane.add(checkmd5btn);
createmd5button.addactionlistener(new actionlistener()
if (!validate3(true))
{
return;
}
sourcefiletf.seteditable(false);
new thread(new runnable()
@override
public void run()
{
createmd5button.setenabled(false);
size_of_file = srcfile.length();
result = systemutil.getfilemd5(srcfile);
checkmd5btn.setenabled(true);
createmd5button.setenabled(true);
}
}).start();
checkmd5btn.addactionlistener(new actionlistener()
// if (!validate3(true))
// return;
long size_of_file2 = srcfile.length();
if (size_of_file2 != size_of_file)
system.out.println("by size");
guiutil23.errordialog(mesg_diff);
checkmd5btn.setenabled(false);
string result2;
sourcefiletf.seteditable(true);
result2 = systemutil.getfilemd5(srcfile);
boolean issame2=issame(result2, result);
if (issame2)
{
guiutil23.infodialog(mesg_same);
}
else
guiutil23.errordialog(mesg_diff);
jlabel targetlabel = new jlabel("target file");
gridbagconstraints gbc_driveclasslabel = new gridbagconstraints();
gbc_driveclasslabel.anchor = gridbagconstraints.west;
gbc_driveclasslabel.insets = new insets(0, 0, 5, 5);
gbc_driveclasslabel.gridx = 1;
gbc_driveclasslabel.gridy = 3;
mainpane.add(targetlabel, gbc_driveclasslabel);
targetfiletf = new jtextfield();
targetfiletf.settext("/home/whuang2/bin/wh_dos2unix");
gridbagconstraints gbc_driveclasstf = new gridbagconstraints();
gbc_driveclasstf.insets = new insets(0, 0, 5, 5);
gbc_driveclasstf.fill = gridbagconstraints.horizontal;
gbc_driveclasstf.gridx = 2;
gbc_driveclasstf.gridy = 3;
mainpane.add(targetfiletf, gbc_driveclasstf);
targetfiletf.setcolumns(10);
browsertargetbtn = new jbutton("browser target");
gridbagconstraints gbc_browsertargetbtn = new gridbagconstraints();
gbc_browsertargetbtn.fill = gridbagconstraints.east;
gbc_browsertargetbtn.insets = new insets(0, 0, 5, 5);
gbc_browsertargetbtn.gridx = 3;
gbc_browsertargetbtn.gridy = 3;
mainpane.add(browsertargetbtn, gbc_browsertargetbtn);
browsertargetbtn.addactionlistener(new actionlistener()
// system.out.println("target");
dialogutil.browser3(targetfiletf,
jfilechooser.files_and_directories, checksameapp.this);
comparebtn = new jbutton("compare");
gridbagconstraints gbc_runbtn = new gridbagconstraints();
gbc_runbtn.fill = gridbagconstraints.east;
gbc_runbtn.insets = new insets(0, 0, 5, 5);
gbc_runbtn.gridx = 4;
gbc_runbtn.gridy = 3;
mainpane.add(comparebtn, gbc_runbtn);
comparebtn.addactionlistener(new actionlistener()
if (!validate3(false))
long size_of_targfile = targfile.length();
long size_of_srcfile = srcfile.length();
if (size_of_targfile != size_of_srcfile)
comparebtn.setenabled(false);
string result_source = systemutil.getfilemd5(srcfile);
string result_target;
result_target = systemutil.getfilemd5(targfile);
boolean issame2=issame(result_source, result_target);
comparebtn.setenabled(true);
c.add(mainpane, borderlayout.center);
// system.out.println(c.getlayout());
//new jscrollpane(text)
// private string create_md5(string filepath)
// {
// try
// {
// return create_md5(filepath, null);
// }
// catch (ioexception e)
// e.printstacktrace();
// return null;
// }
*
* @param result_source : such as b79898bb7907648871745cd5422c79ce /home/whuang2/bin/ab.txt
* @param result_target
* @return
private boolean issame(string result_source, string result_target)
if (result_source == null || result_target == null)
return false;
return (result_source.split("[ \t]")[0].equals(result_target
.split("[ \t]")[0]));
private boolean validate3(boolean isself)
string sourcefile = sourcefiletf.gettext();
string targetfile_dir = targetfiletf.gettext();
if (sourcefile == null || sourcefile.equals(""))
guiutil23
.warningdialog("source file can not be empty,please select again !");
sourcefiletf.requestfocus();//focus
if (!isself)
if (targetfile_dir == null || targetfile_dir.equals(""))
guiutil23
.warningdialog("target file can not be empty,please select again !");
targetfiletf.requestfocus();//focus
return false;
// system.out.println("source file:" + sourcefile);
// system.out.println("target file:" + targetfile_dir);
srcfile = new file(sourcefile);
if (!srcfile.exists())
.warningdialog("source file does not exist,please select again!");
sourcefiletf.selectall();
if (srcfile.exists() && srcfile.isdirectory())
.warningdialog("source file can not be directory,please select again!");
targfile = new file(targetfile_dir);
if (!targfile.exists())
guiutil23
.warningdialog("target file does not exist,please select again!");
targetfiletf.requestfocus();//focus
targetfiletf.selectall();
return false;
if (targfile.exists() && targfile.isdirectory())
.warningdialog("target file can not be directory,please select again!");
return true;
// public static void main2(string[] args)
// string sourcefile = "/home/whuag2/workspace/io0007-find_progess/src/com/cmd/dos/hw/util/cmdutil.java";
// md5(sourcefile);
}
swingwork類:checksameswingworker
package com.hw.util;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.list;
import javax.swing.swingworker;
import com.common.util.myprocess;
public class checksameswingworker extends swingworker<boolean, character>
private bufferedreader br_right = null;
private bufferedreader br_error = null;
private myprocess myprocess = null;
private char word = ' ';
private int tmp = 0;
private boolean isprintverbose = false;
private stringbuffer stringbuf = new stringbuffer();
public checksameswingworker(myprocess myprocess, bufferedreader br)
this.br_right = br;
this.myprocess = myprocess;
public checksameswingworker(myprocess myprocess)
br_right = new bufferedreader(new inputstreamreader(
myprocess.getinputstream()), 4096);
br_error = new bufferedreader(new inputstreamreader(
myprocess.geterrorstream()), 4096);
@override
protected boolean doinbackground() throws exception
while ((tmp = br_right.read()) != -1)
word = (char) tmp;
publish(word);
while ((tmp = br_error.read()) != -1)
if (isprintverbose)
system.out.println("doinbackground() over");
protected void process(list<character> chunks)
for (char temp : chunks)
// system.out.print(temp);
this.stringbuf.append(temp);
public stringbuffer getstringbuf()
return stringbuf;
* main thread can't execute next command(below waitfor())
* until done() is executed
protected void done()
system.out.println("done() is finish");
try
br_right.close();
catch (ioexception e)
e.printstacktrace();
this.myprocess.stoploop();
}