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
|
package outils;
import java.io.*;
import javax.swing.JOptionPane;
public class FileUtils{
private File from;
private File to;
private static long taille;
private static int tailleFic;
private static int tailleReturn;
public FileUtils(File from,File to) throws IOException{
this.from = from;
this.to = to;
this.run();
}
public void run(){
if (from.isFile()) {
try {
copyFile(from, to);
System.out.println(Controleur.getBar().getPb().getValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (from.isDirectory()){
try {
copyDirectory(from, to);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy(final InputStream inStream, final OutputStream outStream, final int bufferSize) throws IOException {
final byte[] buffer = new byte[bufferSize];
int nbRead;
while ((nbRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, nbRead);
/**actualisation de la barre de chargement en rajoutant la taille du buffer pour chaque copie de fichier en cours */
System.out.println("local");
Controleur.getBar().getPb().setValue(Controleur.getBar().getPb().getValue()+bufferSize);
}
}
public static void copyDirectory(final File from, final File to) throws IOException {
if (! to.exists()) {
to.mkdir();
}
final File[] inDir = from.listFiles();
for (int i = 0; i < inDir.length; i++) {
final File file = inDir[i];
new FileUtils(file, new File(to, file.getName()));
}
}
public static void copyFile(final File from, final File to) throws IOException {
if(! to.isDirectory()){
final InputStream inStream = new FileInputStream(from);
final OutputStream outStream = new FileOutputStream(to);
if (from.length() > 0){
copy(inStream, outStream, (int) Math.min(from.length(), 4*1024));
}
inStream.close();
outStream.close();
}else if(to.isDirectory()){
final InputStream inStream = new FileInputStream(from);
final File file = from;
new FileUtils(file, new File(to, file.getName()));
}else{
throw new FileNotFoundException("Le Dossier de destination : - "+ from.toString() +" - N'existe pas");
}
}
public static void calcTaille(final File from){
if(! from.isDirectory()){
taille += from.length();
}else if(from.isDirectory()){
final File[] inDir = from.listFiles();
for (int i = 0; i < inDir.length; i++) {
final File file = inDir[i];
calcTaille(new File(from, file.getName()));
}
}
}
public static int getTaille(){
return (int)taille;
}
public static void setTaille(int valeur){
taille=valeur;
}
static int nbFichier(File monRepertoire){
File[] f = monRepertoire.listFiles();
int x = 0;
for (int i = 0 ; i < f.length ; i++) {
if (f[i].isFile()) {
x++;
}
else{
x+=nbFichier(f[i]);
}
}
return x;
}
} |
Partager