Move files to folders with same name in Java

466 Views Asked by At

I have files and folders with same name(without characters of extensions). I try to move each files to the right folders with following code.

private static void moveFile() {
    File directory = new File(GlobalVariables.DOWNLOAD_FOLDER_ROOT);
    File[] allFiles = directory.listFiles();

    for (File file : allFiles) {
       if (file.isDirectory() == false) {
        String filename = file.getName().substring(0, file.getName().indexOf("."));
        File newDir = new File(filename);

        file.renameTo(new File(newDir.getAbsolutePath() + File.separator + file.getName()));
       }
    }
}

I got error message and there aren't any changes in file structure.

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

I am looking for same functionality as for /f %x in ('dir /ad /b') do move %x*.* %x\ Windows command.

0

There are 0 best solutions below