Java应用程式因复制大量档案而当机

时间:2018-11-01 14:35:20

标签: java crash

我有一个应用程序,可以将多个文件从目录复制到某个目标位置。问题是,当您选择一个大文件夹时,它对应用程序本身的影响更大,然后崩溃。有什么办法可以使其不崩溃?也许将它分成较小的部分?

这是我的代码:

public void startProcess(File orgDir, File destDir) {

    Screen1Controller sf = new Screen1Controller();

    String selectedExtension = sf.selectedExtension; // Gets selected extension from Screen1
    String extensionType = sf.typeOfExtension; // Gets selected extension type from Screen1

    int y = 1; // This is for searching for duplicates.. See below.


    try {
        File[] files = orgDir.listFiles();

                for (File file : files) {  // Goes through the files in the given directory
                if (!file.isDirectory() && file.getName().endsWith(selectedExtension)){

                    File destinationPath = new File(destDir.getCanonicalPath() + "\\");

                    destDir = new File(destinationPath + "\\" + extensionType);  // Sets the destination path
                    destDir.mkdir();

                    System.out.println("file:" + file.getCanonicalPath());  // Prints the file path


                    try{
                        String fileNameWithOutExt = file.getName().replaceFirst("[.][^.]+$", "");  // Gets the current file without the extension

                        File destFile = new File(destDir.getPath() + "\\" + file.getName());  // If a file of the same name exists in the dest folder


                        if (Files.exists(Paths.get(destFile.getPath())))  // Checks if there is a file with the same name in the folder
                        {
                            System.out.println("There is a duplicate.");

                            File[] destFiles = destDir.listFiles();
                            for (File destinationFile : destFiles)  // Searches through the destination folder
                            {
                                if(destinationFile.getName().startsWith(fileNameWithOutExt)){  // Checks if the selected file has the same name as the file that's going to be moved.
                                    y++;  // Increments y by 1 to keep track of how many there are of the same/similar name
                                }
                            }

                            File newFile = new File(orgDir.getPath() + "\\" + fileNameWithOutExt + "." + y + selectedExtension);  // Creates a new file with new name.

                            file.renameTo(newFile);  // Renames to a unique name and moves the file to the destination folder

                            File destPath = new File(destDir.getPath() + "\\" + newFile.getName());  // Gets the destination path for the file

                            System.out.println(newFile.getCanonicalPath());

                            Files.copy(Paths.get(newFile.getCanonicalPath()), Paths.get(destPath.getPath()));  // Renames the original file back to its original name

                            newFile.renameTo(new File(orgDir.getPath() + "\\" + fileNameWithOutExt + selectedExtension));

                        } else {
                            Files.copy(Paths.get(file.getPath()), Paths.get(destFile.getPath()));  // Moves the file to the destination folder
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                    }
                } else{
                    startProcess(file, destDir);
                }
        }           

    } catch (IOException e) {
    e.printStackTrace();
    }


} 

}

1 个答案:

答案 0 :(得分:0)

我假设您的程序正确,这纯粹是内存问题。通过运行带有选项-Xms 1024m -Xmx 1g的程序来增加内存设置,必要时增加值,请注意总的可用内存。 很高兴看到堆栈的异常跟踪,以确保确定导致崩溃的原因。