LibGDX FileHandle:重命名文件会删除该文件

时间:2017-12-12 21:30:53

标签: java libgdx

要重命名我使用的文件

FileHandle#moveTo(FileHandle dest)

在大多数情况下都可以正常工作。但是当我尝试重命名时,例如,一个文件" abc"到" ABC",文件被删除。 我认为问题是文件名不敏感(至少在桌面,Windows上)。 这是上面提到的方法的实现(我在代码中留下了注释):

public void moveTo (FileHandle dest) {
    if (type == FileType.Classpath) throw new GdxRuntimeException("Cannot move a classpath file: " + file);
    if (type == FileType.Internal) throw new GdxRuntimeException("Cannot move an internal file: " + file);
    copyTo(dest); // file is not copied into another file, since "abc" file  is the same as the dest "ABC" file
    delete(); // and here the "original" file is deleted, but in this case original file equals to dest file, so the file is lost
    if (exists() && isDirectory()) deleteDirectory();
}

问题:

1)这种行为是故意的吗?老实说,感觉不对。

2)这样做是否可以重命名(在这种情况下有效,但也许还有其他注意事项):

FileHandle src = ...;
FileHandle dest = ...;
src.file().renameTo(dest.file());

如果没有,那么正确的方法是什么?

更新

正如@exenza建议的那样,在LibGDX问题跟踪器上打开了issue

1 个答案:

答案 0 :(得分:2)

在Windows上,文件名不区分大小写。这意味着" abc"和" ABC"引用同一个文件。您的copyTo()电话会将文件复制到自己。然后delete()删除该文件。在所有这些期间,只有一个文件,没有副本。