lunes, 21 de mayo de 2018

Copia seguridad en Java



 Seguidamente se muestra como hacer copia de carpetas de un origen a destino en Java. Sino existe la carpeta la crea. Son dos ficheros: Backup.java y FileUtils.java



Backup.java
=====================================================
package backup;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Backup {
 public static void main(String[] args) {

        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            String dia = dateFormat.format(cal.getTime());

            File srcDir;
            File destDir;
            FileUtils fileUtils;
            fileUtils = new FileUtils();
            int contador = 1;

            System.out.println("Fecha: " + dia);

            srcDir = new File("C:\\Users\\Paco\\Documents\\Fuente");
            destDir = new File("C:\\Users\\Paco\\Documents\\backup\\" + dia + "\\Fuente");
            fileUtils.copyDirectory(srcDir, destDir);
            System.out.println(contador + ". Copiado " + srcDir + " " + " en " + destDir);
           
        } catch (IOException ex) {
            Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
FileUtils.java
=====================================================
package backup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class FileUtils {

    public void copy(File sourceLocation, File targetLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            copyDirectory(sourceLocation, targetLocation);
        } else {
            copyFile(sourceLocation, targetLocation);
        }
    }

    void copyDirectory(File source, File target) throws IOException {
        File destino;
        File origen;

        if (!target.exists()) {
            //target.mkdir();
            target.mkdirs(); // Crea carpetas sino existe
        }

        for (String f : source.list()) {
            origen = new File(source, f);
            destino = new File(target, f);
            if ((origen.lastModified() > destino.lastModified()) || (origen.length() > destino.length())) {
                copy(origen, destino);
            }
        }
    }

    private void copyFile(File source, File target) throws IOException {
        try (
                InputStream in = new FileInputStream(source);
                OutputStream out = new FileOutputStream(target)) {
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0) {
                out.write(buf, 0, length);
            }
        }
    }
}



Fuente:
https://stackoverflow.com/questions/5368724/how-to-copy-a-folder-and-all-its-subfolders-and-files-into-another-folder