file Content Equals - Java File Path IO

Java examples for File Path IO:File Operation

Description

file Content Equals

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file1 = new File("Main.java");
        File file2 = new File("Main.java");
        System.out.println(fileEquals(file1, file2));
    }//from   w  w  w. j  av  a 2s  . co m

    public static boolean fileEquals(File file1, File file2) {
        try {
            FileInputStream f1 = new FileInputStream(file1);
            FileInputStream f2 = new FileInputStream(file2);
            byte[] b1 = new byte[(int) file1.length()];
            byte[] b2 = new byte[(int) file2.length()];
            f1.read(b1);
            f2.read(b2);
            for (int j = 0; j < b1.length; j++) {
                if (b1[j] != b2[j])
                    return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Related Tutorials