file_teedd

package com.test.junin;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.math.BigInteger;

import java.security.MessageDigest;

 

public class FileOperationTest 

{

public static void main(String[] args)

{

//String tempDirectory = new String("H:\\Temp");

//

//File dir = new File(tempDirectory);

//

//File file;

//

//FileUtil.deleteOldGeneratedReportDesignerFile(tempDirectory);

//

//try

//{

//    File.createTempFile("Report-Designer-", ".pdf",dir);

//    File.createTempFile("Report-Designer-", ".pdf",dir);

//}

//catch(IOException IOE)

//{

//IOE.printStackTrace();

//}

//

//String fileName = FileUtil.getNewGeneratedReportDesignerFileName(tempDirectory);

//

//System.out.println(fileName);

//

//file = FileUtil.getNewGeneratedReportDesignerFile(tempDirectory);

//

//System.out.println(file.toString());

 

String dirName = "H:\\Temp";

 

File dir = new File(dirName);

 

String[] fileList = dir.list();

 

File filemd1 = new File(dirName+File.separator+fileList[0]);

 

for(int i = 0;i<fileList.length;i++)

{

File filemd = new File(dirName+File.separator+fileList[i]);

 

 

 

String md5 = FileDigest.getFileMD5(filemd);

 

System.out.println("---------------------------------");

System.out.println(filemd.toString());

System.out.println(md5);

 

System.out.println(FileUtil.compareFile(filemd, filemd1));

}

 

 

 

}

}

 

class FileUtil

{

    public static void deleteOldGeneratedReportDesignerFile(String tempDirectory)

    {

        File tempDir = new File(tempDirectory);

        File oldReportFile;

        String[] fileList = tempDir.list();

        for(int num = 0;num < fileList.length;num++)

        {

        if(fileList[num].contains("Report-Designer-"))

        {

        oldReportFile = new File(tempDirectory+File.separator+fileList[num]);

        oldReportFile.delete();

        }

        } 

    }

    

    public static File getNewGeneratedReportDesignerFile(String tempDirectory)

    {

        File tempDir = new File(tempDirectory);

        File newReportFile;

        String[] fileList = tempDir.list();

        int fileIndex = 0;

        

        for(int num = 0;num < fileList.length;num++)

        {

        if(fileList[num].contains("Report-Designer-"))

        {

        fileIndex = num;

        }

        } 

        newReportFile = new File(tempDirectory+File.separator+fileList[fileIndex]);

        return newReportFile;

    }

    

    

    public static String getNewGeneratedReportDesignerFileName(String tempDirectory)

    {

    File tempDir = new File(tempDirectory);

        String[] fileList = tempDir.list();

        int fileIndex = 0;

        

        for(int num = 0;num < fileList.length;num++)

        {

        if(fileList[num].contains("Report-Designer-"))

        {

        fileIndex = num;

        }

        } 

        return fileList[fileIndex];

    }

    

    public static boolean compareFile(File file1,File file2)

    {

    return getFileMD5(file1).equals(getFileMD5(file2));

    }

    

    

    public static String getFileMD5(File file) {

        if (!file.isFile()){

          return null;

        }

        MessageDigest digest = null;

        FileInputStream in=null;

        byte buffer[] = new byte[1024];

        int len;

        try {

          digest = MessageDigest.getInstance("MD5");

          in = new FileInputStream(file);

          while ((len = in.read(buffer, 0, 1024)) != -1) {

            digest.update(buffer, 0, len);

          }

          in.close();

        } catch (Exception e) {

          e.printStackTrace();

          return null;

        }

        BigInteger bigInt = new BigInteger(1, digest.digest());

        return bigInt.toString(16);

      }

    

    

    

    

    

    

    

    

    

    

    

}

你可能感兴趣的:(File)