Create some files and folders that not exist

50 views Asked by At

I should copy some files and I have a JTextField for destination file. User can enter a file path or choose a file by a JFileChooser, It is doing well, but when I enter an incorrect input. such as this: C:\Users\123\123\123 or something like this: 123 The program will crash and it cannot exit the while loop. I have no idea about how can I fix this problem, My createFile() function is here:

private void createFile(File f) {
        if (f.exists()) {
            return;
        } else {
            while (true) {
                System.out.println(1);
                if (f.getParentFile().exists()) {
                    if (f.getName().equals(dest.getName())) {
                        return;
                    } else {
                        System.out.println(f.mkdirs() + " Directory created ");
                    }
                } else {
                    System.out.println(f.getPath());
                    createFile(f.getParentFile());
                }
                if(f.exists()){
                    break;
                }
            }
        }
    }
0

There are 0 answers