1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
import java.util.*;
import java.io.*;
public class USBDaemon implements Runnable {
static Boolean USBConnect = false;
static Boolean CopieOK = false;
static int compteur = 0;
static int tempo = 0;
static String extention;
static String dossier;
static String dossierSauvegarde;
static String Dossiers[];
public USBDaemon (String arg[]){
this.compteur = 0;
this.tempo = Integer.parseInt(arg[3]);
this.extention = arg[0];
this.dossier = arg[1];
this.dossierSauvegarde = arg[2];
Thread t = new Thread(this);
t.start();
}
public void attente() {
//System.out.println("Recherche dans : "+this.dossier);
File fichierDossier = new File(this.dossier);
if(fichierDossier.isDirectory()){
this.USBConnect = true;
// System.out.println("Clef USB connectée!");
}
else if (this.CopieOK) {
this.USBConnect = false;
this.CopieOK = false;
// System.out.println("Pas de clef USB détectée.");
}
else {
// System.out.println("Pas de clef USB détectée.");
}
}
public void compteur() {
//System.out.println("Vérification supplémentaire.");
File fichierDossier = new File(this.dossier);
if(fichierDossier.isDirectory()){
this.compteur++;
// System.out.println("Toujours connecté");
}
else {
this.USBConnect = false;
this.compteur = 0;
// System.out.println("Déconnecté.");
}
}
public void recherche() {
try {
this.CopieOK = true;
System.out.println("Epluchage des dossiers.");
File fichierDossier;
String contenu[];
File file;
String Temp[];
String FichierACopier[];
this.Dossiers = new String[1];
this.Dossiers[0] = "";
int i = 0;
while (i < Dossiers.length) {
//System.out.println(Dossiers[i]);
fichierDossier = new File(this.dossier+File.separator+Dossiers[i]);
contenu = fichierDossier.list();
for(int j = 0; j < contenu.length; j++){
file = new File(this.dossier+Dossiers[i]+File.separator+contenu[j]);
if(file.isDirectory()){
Temp = new String[Dossiers.length + 1];
for(int k = 0; k < Dossiers.length; k++){
Temp[k] = Dossiers[k];
}
Temp[Dossiers.length] = Dossiers[i]+File.separator+contenu[j];
Dossiers = Temp;
}
}
i++;
}
GregorianCalendar d = new GregorianCalendar();
String time = d.get(Calendar.DAY_OF_MONTH)+"-"+d.get(Calendar.MONTH)+"-"+d.get(Calendar.HOUR)+"-"+d.get(Calendar.MINUTE)+"-"+d.get(Calendar.SECOND);
FileInputStream read;
FileOutputStream write;
byte[] tabLu;
int nbLu;
File destination;
i = 0;
while (i < Dossiers.length) {
fichierDossier = new File(this.dossier+Dossiers[i]);
contenu = fichierDossier.list();
for (int j = 0; j < contenu.length; j++) {
if (contenu[j].indexOf(this.extention) != -1) {
System.out.println("Sauvegarde du fichier : "+this.dossier+Dossiers[i]+File.separator+contenu[j]);
System.out.println("Dans : "+this.dossierSauvegarde+File.separator+time+Dossiers[i]+File.separator+contenu[j]);
destination = new File(this.dossierSauvegarde+File.separator+time+Dossiers[i]+File.separator);
destination.mkdirs();
destination = new File(this.dossierSauvegarde+File.separator+time+Dossiers[i]+File.separator+contenu[j]);
file = new File(this.dossier+Dossiers[i]+File.separator+contenu[j]);
read = new FileInputStream(file);
write = new FileOutputStream(destination);
tabLu = new byte[10240];
while((nbLu = read.read(tabLu)) > 0){
write.write(tabLu,0,nbLu);
}
}
}
i++;
}
this.CopieOK = true;
System.out.println("Fini!");
}
catch ( java.io.FileNotFoundException e) { //102 - 103
}
catch ( java.io.IOException e) { // 107
}
}
public void run() {
while (true) {
try {
if (!this.USBConnect || this.CopieOK) {
this.attente();
Thread.sleep(60000);
}
else if (this.compteur < this.tempo) {
this.compteur();
Thread.sleep(60000);
}
else if (!this.CopieOK) {
this.recherche();
Thread.sleep(60000);
}
}
catch (InterruptedException e) { }
}
}
// java USBDaemon .ppt g: c:\sauvegarde 5
// java USBDaemon .ppt /media/sda1 /dd/sauvegarde 5
public static void main(String arg[]){
USBDaemon start = new USBDaemon(arg);
}
} |
Partager