0

I'm trying to get a backup program to take a folder selected by the user using JFileChooser and copy it to a destination also selected using the same method.

The only problem is that it doesn't put all the contents of the selected folder into a folder in the destination named the same thing, and I really dont know why. I did some Googling, and I didnt find anything useful.

Here's the code:

package main;

import java.io.File;
import java.util.ArrayList;

public class test {

BackgroundWorker bw;
static ArrayList bgWorker = new ArrayList();
ArrayList al = new ArrayList(); // this is the list of files selected to
                                // back up
String dir = ""; // this is the path to back everything up to selected by
                    static // the user
boolean bwInitiallized = false;

public void startBackup() throws Exception {
    Panel.txtArea.append("Starting Backup...\n");

    for (int i = 0; i < al.size(); i++) {
        /**
         * THIS IS WHERE I NEED TO CREATE THE FOLDER THAT EACH BACKUP FILE
         * WILL GO INTO EX: SC2 GOES INTO A FOLDER CALLED SC2 AND RIOT GOES
         * TO RIOT, ALL WITHIN THE DIRECTORY CHOSEN
         */
        File file = new File((String) al.get(i));
        File directory = new File(dir);

        // File dirFile = new File(dir + "\\" + file.getName());
        // if (!dirFile.exists())
        // dirFile.mkdir();

        bw = new BackgroundWorker(Panel.txtArea, file, directory);
        bgWorker.add(bw);
        bwInitiallized = true;
        bw.execute();

        /**
         * follows to the bottom of the txtarea
         */
        int x;
        Panel.txtArea.selectAll();
        x = Panel.txtArea.getSelectionEnd();
        Panel.txtArea.select(1, x);

    }
    clearList(); // method not included in this example that deletes all the
                    // contents of the al array list.
}

public static void cancel() {
    BackgroundWorker bg;
    if (bwInitiallized) {
        bwInitiallized = false;
        Panel.txtArea.append("Cancelling...\n");
        for (int i = 0; i < bgWorker.size(); i++) {
            // BackgroundWorker bg = (BackgroundWorker) bgWorker.get(i);
            bg = (BackgroundWorker) bgWorker.get(i);
            bg.cancel(true);
        }
        Panel.txtArea.append("Canceled backUp!\n");
    } else {
        Panel.txtArea.append("Cannot Cancel! Not Initiallized!\n");
    }
}
}

What I think the problem is: I believe that for whatever reason the destination file path needs to have to the name of the folder included, but I tried that and it didnt help.

Does anyone know what I'm doing wrong?

This is the code that makes the JFileChooser:

public void fileChooserToDestination() {
    LookAndFeel previousLF = UIManager.getLookAndFeel();
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    JFileChooser jfc = new JFileChooser();
    try {
        UIManager.setLookAndFeel(previousLF);
    } catch (UnsupportedLookAndFeelException e) {
    }

    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    if (jfc.showDialog(null, "Select Directory") == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        dir = file.getPath();
        Panel.txtArea.append("User selected " + file.getPath()
                + " for the destination...\n");
        try {
            startBackup();
        } catch (Exception e) {
        }

    } else {
        Dialogs.msg("You canceled selecting a destination folder! Returning to main screen...");
        al.clear();
        Panel.txtArea.append("User cancelled the destination selection..."
                + "\n");
    }

    return;
}
1
  • 1
    Really need to see the code that determines the value for dir Aug 28, 2012 at 2:17

1 Answer 1

1

Parts of the code I need are missing. I can't see where you're making your decisions about how to append the source file to the destination path, so I wrote this quick example it illustrate the point...

File sourcePath = new File("/path/to/be/backed/up");
File destPath = new File("X:/BackupHere");

// Get all the files from sourcePath
List<File> listFiles = getFilesFrom(sourcePath);

for (File toBackup : listFiles) {

    // Now we need to strip off the sourcePath
    // Get the name of the file
    String fileName = toBackup.getName();
    // Get parent folder's path
    String path = toBackup.getParent();
    // Remove the source path from file path
    path = path.substring(sourcePath.getPath().length());

    // Append the file name to the path
    path = path + File.separator + fileName;

    // Now we have the name of the back up file
    String backupFile = destPath + path;

    System.out.println("Backup to " + backupFile);

}

Basically, you need to strip of the "source path" (the directory you want to copy). You then use the resulting value to append to the "backup path" value and you should then have a suitable path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.