0

我有同名的文件和文件夹(没有扩展名字符)。我尝试使用以下代码将每个文件移动到正确的文件夹中。

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()));
       }
    }
}

我收到错误消息,文件结构没有任何变化。

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1

我正在寻找与for /f %x in ('dir /ad /b') do move %x*.* %x\Windows 命令相同的功能。

4

0 回答 0