比较两个文件夹下文件的数量,名称和大小?谢谢!:)

解决方案 »

  1.   

    我的解决方法是这样的,供大家参考,也请高手给予批评指正:
     package test;import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Arrays;import jcifs.smb.SmbException;
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileOutputStream;
    import hz365.JDBCConnInfo;/**
     * 检查2个文件夹下的文件是否一样(数量,名称,大小),定时自动检查 1.检查文件的总数是否一致 2.检查文件的名称是否一致
     * 3.检查相同名称的文件大小是否一致
     * 
     * @author echo
     * 
     */
    public class FileTest { public String  testFile(File file1, File file2) {
    String path1 = "";
    String path2 = "";
    String msg = "";
    path1 = file1.getPath();
    path2 = file2.getPath(); if (file1.isDirectory()) {
    if (file2.isDirectory()) {
    String[] fileList1 = file1.list();
    String[] fileList2 = file2.list();
    boolean flag = false; if (fileList1.length != fileList2.length) {
    msg = path1 + "和" + path2 + "这2个文件夹下的文件数目不一致!" + "\n";
    // System.out.print(msg);
    } else {
    flag = Arrays.equals(fileList1, fileList2);// 判断各个文件名称是否一致,完全一致的话为true,不一致为false
    // Arrays.sort(fileList1);//对文件名称进行排序
    if (!flag) {
    msg = path1 + "和" + path2 + "存在不一样的文件" + "\n";
    // System.out.print(msg);
    } else {// 判断一样文件名称的文件大小是否相同
    for (int i = 0; i < fileList1.length; i++) {
    File file3 = new File(path1 + "\\" + fileList1[i]);
    File file4 = new File(path2 + "\\" + fileList2[i]);
    if (file3.isFile()) {// file3是文件的话,file4也必然是文件
    if (file3.length() != file4.length()) {
    msg = file3 + "和" + file4 + "这2个文件大小不一样"+ "\n";
    // System.out.print(msg);
    // sendSms("13817318125", msg, "80", 0);
    }
    } else {// 如果file3和file4这一层是文件夹的话,就对这一层的文件夹再进行检查
    testFile(file3, file4);
    }
    }
    }
    }
    } else {
    msg = path2 + "is not a directory!!" +"\n" ; }
    } else {
    msg = path1 + "is not a directory!!" +"\n" ;
    // System.out.print(msg);
    }
    return msg;
    }
    }