>  기사  >  Java  >  파일이 동일한지 확인하는 Java 방법

파일이 동일한지 확인하는 Java 방법

尚
원래의
2019-12-12 16:26:332642검색

파일이 동일한지 확인하는 Java 방법

Java에서 파일이 동일한지 판단하는 방법:

1. MD5 또는 SHA-1을 계산한 다음 비교하여 판단

 // 计算文件的 MD5 值 根据MD5值 判断文件是否是同一个文件
public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest =MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            return bigInt.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
// 计算文件的 SHA-1 值 根据SHA-1值 判断文件是否是同一个文件
    public static String getFileSha1(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest =MessageDigest.getInstance("SHA-1");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            return bigInt.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

2 내용이 동일한지 직접 판단

 public class  IOOperation  
{  
    public static void main(String[] args)   
    {  
        FileInputStream File1 = null;  
        FileInputStream File2 = null;  
        BufferedReader in = null;  
        String sFile;  

        if(args.length != 2)  
        {  
            System.out.println("The command line should be: java IOOperation testX.txt testX.txt");  
            System.out.println("X should be one of the array: 1, 2, 3");  
            System.exit(0);  
        }  

        try  
        {  
            File1 = new FileInputStream(args[0]);  
            File2 = new FileInputStream(args[1]);  

            try  
            {  

                if(File1.available() != File2.available())  
                {  
                   //长度不同内容肯定不同  
                    System.out.println(args[0] + " is not equal to " + args[1]);  
                }  
                else  
                {  
                    boolean tag = true;  

                    while( File1.read() != -1 && File2.read() != -1)  
                    {  
                        if(File1.read() != File2.read())  
                        {  
                            tag = false;  
                            break;  
                        }  
                    }  

                    if(tag == true)  
                        System.out.println(args[0] + " equals to " + args[1]);  
                    else  
                        System.out.println(args[0] + " is not equal to " + args[1]);  
                }  
            }  
            catch(IOException e)  
            {  
                System.out.println(e);  
            }  
        }  
        catch (FileNotFoundException e)  
        {  
            System.out.println("File can't find..");  
        }  
        finally  
        {  

            try  
            {  
                if(File1 != null)  
                    File1.close();  
                if(File2 != null)  
                    File2.close();  
            }  
            catch (IOException e)  
            {  
                System.out.println(e);  
            }  
        }  
    }

지식이 있으신 분들은 java basic tutorial 칼럼을 주목해주세요.

위 내용은 파일이 동일한지 확인하는 Java 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.