2014-11-07 1 views
1

두 파일을 바이트 블록으로 비교하려고합니다. 블록 단위로 차단하십시오. 하지만 루프 트리에 문제가 있습니다.두 파일을 바이트 블록으로 비교 Java

public void compare() { 
     File file1 = arrayOfFiles.get(i); 
     File file2 = arrayOfFiles.get(y); 
     if (file1.length() != file2.length()) { 
      break; 
     } 
     else 
     { 
      for (int z = 0; ; z++) { 
       byte[] b1 = getParts(file1, z); 
       byte[] b2 = getParts(file2, z); 
       if (b1.length != b2.length) { 
        break; 
       } 
       else 
       { 
        for (int x = 0; ; x++) { 
         if (b1[x] != b2[x]) { 
          break; 
         } 
         else 
         { 
          //how can I find the end of file? and compare last [x] of b1 and b2? 
         } 
        } 
       } 
      } 
     } 
    } 

    private static byte[] getParts(File file, int z) throws IOException { 
     byte [] bytes = new byte[1024]; 
     int point = z * 1024; 
     RandomAccessFile raf = new RandomAccessFile(file, "r"); 
     FileChannel fc = raf.getChannel(); 
     MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, point, 1024); 
     buffer.get(bytes); 
     buffer.clear(); 
     return bytes; 
    } 

두 파일을 바이트 단위로 비교하고 크기가 다른 블록을 사용하는 다른 방법이 있습니까?

답변

1

파일의 마지막 바이트 블록을 비교하려면 사소한 수정으로 인한 이점을 얻을 수 있습니다. 마지막 블록에서 블록 반복을 시작하십시오. for 절을 다음과 같이 변경하여 역순으로 반복합니다.

import java.lang.Math; 
import java.nio.channels.FileChannel; 
import java.io.File; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer; 

public class Compare 
{ 
    public static final int BLOCK_SIZE = 1024; 

    public boolean compare(File file1, File file2) 
    { 
     //File file1 = arrayOfFiles.get(i); 
     //File file2 = arrayOfFiles.get(y); 
     boolean equal = file1.length() != file2.length(); 
     for (int z = getSizeInBlocks(file1) - 1; equal && 0 <= z ; z--) 
     { 
      MappedByteBuffer b1 = getParts(file1, z); 
      MappedByteBuffer b2 = getParts(file2, z); 
      if (b1.remaining() != b2.remaining()) 
      { 
       equal = false; 
       break; 
      } 
      else 
      { 
       for (int x = getBlockSize() - 1; equal && 0 <= x; x--) 
       { 
        if (b1[x] != b2[x]) 
        { 
         equal = false; 
         break; 
        } 
        else 
        { 
         //how can I find the end of file? and compare last [x] of b1 and b2? 
        } 
       } 
      } 
     } 
     return equal; 
    } 

    private static int getSizeInBlocks(File file) 
    { 
     return (int) Math.ceil((double)getBlockSize()/file.length()); 
    } 

    private static int getBlockSize() 
    { 
     return BLOCK_SIZE; 
    } 

    private static ByteBuffer getParts(File file, int z) 
     throws IOException 
    { 
     int point = z * getBlockSize(); 
     RandomAccessFile raf = new RandomAccessFile(file, "r"); 
     FileChannel  fc  = raf.getChannel(); 
     MappedByteBuffer buffer = fc.map(
            FileChannel.MapMode.READ_ONLY, 
            point, 
            getBlockSize()); 
     return buffer; 
    } 
}