JComparator - Comparator.java
Cerca
 











Comparator.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. package jcomparator;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12.  *
  13.  * @author Paolo
  14.  */
  15. public class Comparator implements Runnable {
  16.  
  17.     private File fileOne;
  18.     private File fileTwo;
  19.     private int equalByte;
  20.     private double percentEqual;
  21.     private Thread startThread;
  22.     private double percentCharging;
  23.  
  24.     public Comparator(File pFileOne, File pFileTwo) {
  25.         this.fileOne = pFileOne;
  26.         this.fileTwo = pFileTwo;
  27.         this.equalByte = 0;
  28.         this.percentEqual = 0.0;
  29.         this.percentCharging = 0.0;
  30.     }
  31.  
  32.     public void start() {
  33.         this.startThread = new Thread(this);
  34.         this.startThread.start();
  35.     }
  36.  
  37.     public boolean isAlive() {
  38.         return this.startThread.isAlive();
  39.     }
  40.  
  41.     public void run() {
  42.         try {
  43.             FileInputStream inOne = new FileInputStream(this.fileOne);
  44.             FileInputStream inTwo = new FileInputStream(this.fileTwo);
  45.             DataInputStream inputOne = new DataInputStream(inOne);
  46.             DataInputStream inputTwo = new DataInputStream(inTwo);
  47.             long lengOne = this.fileOne.length();
  48.             long lengTwo = this.fileTwo.length();
  49.             long minLeng = (lengOne < lengTwo ? lengOne : lengTwo);
  50.             long maxLeng = (lengOne > lengTwo ? lengOne : lengTwo);
  51.             byte[] bufferOne = new byte[(int) lengOne];
  52.             byte[] bufferTwo = new byte[(int) lengTwo];
  53.             try {
  54.                 inputOne.read(bufferOne);
  55.                 inputTwo.read(bufferTwo);
  56.             } catch (IOException ex) {
  57.             }
  58.             for (int i = 0; i < minLeng; i++) {
  59.                 if (bufferOne[i] == bufferTwo[i]) {
  60.                     this.equalByte++;
  61.                     this.percentEqual = (double) (this.equalByte * 100) / maxLeng;
  62.                     if (this.percentEqual < 0.001) {
  63.                         this.percentEqual = 0;
  64.                     }
  65.                 }
  66.                 this.percentCharging = (double) (i * 100) / (minLeng-1);
  67.                 if (this.percentCharging < 0.001) {
  68.                     this.percentCharging = 0;
  69.                 }
  70.                 try {
  71.                     Thread.sleep(1);
  72.                 } catch (InterruptedException ex) {
  73.                 }
  74.             }
  75.  
  76.         } catch (FileNotFoundException ex) {
  77.         }
  78.     }
  79.  
  80.     public int getEqualByte() {
  81.         return equalByte;
  82.     }
  83.  
  84.     public String getPercentEqual() {
  85.         String percent = this.percentEqual + "";
  86.         String[] split = new String[2];
  87.         split[0] = "";
  88.         split[1] = "";
  89.         boolean afterPoint = false;
  90.         for (int i = 0; i < percent.length(); i++) {
  91.             if (percent.charAt(i) == '.') {
  92.                 afterPoint = true;
  93.             }
  94.             if (!afterPoint) {
  95.                 split[0] = split[0] + percent.charAt(i);
  96.             } else {
  97.                 split[1] = split[1] + percent.charAt(i);
  98.             }
  99.         }
  100.         String retval = split[0] + split[1].substring(0, 2);
  101.         return retval;
  102.     }
  103.  
  104.     public String getPercentCharging() {
  105.         String percent = this.percentCharging + "";
  106.         String[] split = new String[2];
  107.         split[0] = "";
  108.         split[1] = "";
  109.         boolean afterPoint = false;
  110.         for (int i = 0; i < percent.length(); i++) {
  111.             if (percent.charAt(i) == '.') {
  112.                 afterPoint = true;
  113.             }
  114.             if (!afterPoint) {
  115.                 split[0] = split[0] + percent.charAt(i);
  116.             } else {
  117.                 split[1] = split[1] + percent.charAt(i);
  118.             }
  119.         }
  120.         String retval = split[0] + split[1].substring(0, 2);
  121.         return retval;
  122.     }
  123. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.