-1

I have an application which copies files from one directory to another. But every time I call the method to copy the files the UI (window) freezes until they´re done being copied. Do you have any idea why? It doesn't matter how many files that are in the directory as it always freezes. This is my code:

public void startProcess(File orgDir, File destDir) {
    Screen1Controller sf = new Screen1Controller();
    int y = 1;
    try {
        File[] files = orgDir.listFiles();
        for (File file : files) {
            if (file.isDirectory() == false) {
                File destinationPath = new File(destDir.getCanonicalPath() + "\\");
                destDir = new File(destinationPath + "\\" + "");
                destDir.mkdir();
                System.out.println("file:" + file.getCanonicalPath());
                try {
                    String fileNameWithOutExt = file.getName().replaceFirst("[.][^.]+$", "");
                    File destFile = new File(destDir.getPath() + "\\" + file.getName());
                    if (Files.exists(Paths.get(destFile.getPath()))) {
                        System.out.println("There is a duplicate.");
                        File[] destFiles = destDir.listFiles();
                        for (File destinationFile : destFiles) {
                            if (destinationFile.getName().startsWith(fileNameWithOutExt)) {
                                y++;
                            }
                        }
                        File newFile = new File(orgDir.getPath() + "\\" + fileNameWithOutExt + "." + y);

                        file.renameTo(newFile);
                        File destPath = new File(destDir.getPath() + "\\" + newFile.getName());
                        System.out.println(newFile.getCanonicalPath());
                        Files.copy(Paths.get(newFile.getCanonicalPath()), Paths.get(destPath.getPath()));

                        newFile.renameTo(new File(orgDir.getPath() + "\\" + fileNameWithOutExt));
                    } else {
                        Files.copy(Paths.get(file.getPath()), Paths.get(destFile.getPath()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                startProcess(file, destDir);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    What GUI library are you using? Swing? JavaFX? – Hovercraft Full Of Eels Nov 01 '18 at 22:21
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Nov 01 '18 at 23:45

1 Answers1

2

You have to create a new Thread to handle the copying in a new thread and not the UI thread

    new Thread(new Runnable() {
        @Override
        public void run() {

        }
    }).start();

put your method inside that, the one that is copying the files.

so it would look something like

    new Thread(() -> {
        startProcess(... , ...);
    }).start();
SamHoque
  • 2,978
  • 2
  • 13
  • 43