Skip to content

Instantly share code, notes, and snippets.

@toboqus
Created December 31, 2015 14:49
Show Gist options
  • Save toboqus/38def3a9b7b61239f8a7 to your computer and use it in GitHub Desktop.
Save toboqus/38def3a9b7b61239f8a7 to your computer and use it in GitHub Desktop.
Simple method of replicating the file structure inc files from one drive/directory to another.
package backup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Backup {
private File SourceDrive;
private File TargetDrive;
public static void main(String[] args) {
// TODO Auto-generated method stub
Backup process = new Backup();
process.start();
}
public Backup(){
this.SourceDrive = new File("H:\\");
this.TargetDrive = new File("I:\\");
}
public void start(){
if(SourceDrive == null || !SourceDrive.exists()){
System.out.println("ERROR: The source drive/directory does not exist.");
System.exit(0);
}
if(TargetDrive == null || !TargetDrive.exists()){
System.out.println("ERROR: The target drive/directory does not exist.");
System.exit(0);
}
long start = System.nanoTime();
processSource(SourceDrive);
processTarget(TargetDrive);
long finish = System.nanoTime();
System.out.println("Execution time = " + (finish-start)/1000000f + " milliseconds ");
}
/**
* get relative path in respect to either the source directory
* or the target directory.
*
* @param path
* @return
*/
private String getRelativePath(String path){
String result = null;
if(path.startsWith(SourceDrive.getAbsolutePath())){
result = path.substring(SourceDrive.getAbsolutePath().length());
}else if(path.startsWith(TargetDrive.getAbsolutePath())){
result = path.substring(TargetDrive.getAbsolutePath().length());
}
return result;
}
public void processSource(File source){
File targetFile = new File(TargetDrive+getRelativePath(source.getAbsolutePath()));
if(source.isFile()){
if(targetFile.exists()){
if(targetFile.lastModified() < source.lastModified()){
//replace file
System.out.println("Replaced in target: "+getRelativePath(source.getAbsolutePath()));
try {
copyFile(source, targetFile);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}else{
//create and copy file/directory
System.out.println("Created in target: "+getRelativePath(source.getAbsolutePath()));
try {
copyFile(source, targetFile);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}else{
targetFile.mkdirs();
for(File f: source.listFiles()){
processSource(f);
}
}
}
/**
* this method will delete any files stored on the target
* that are not present in the source
*/
public void processTarget(File target){
File sourceFile = new File(SourceDrive+getRelativePath(target.getAbsolutePath()));
if(target.isDirectory()){
for(File f: target.listFiles()){
processTarget(f);
}
}
if(!sourceFile.exists()){
System.out.println("Removed from target: "+getRelativePath(target.getAbsolutePath()));
target.delete();
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment