简体   繁体   中英

LibGDX FileHandle: Renaming a file deletes the file

To rename a file I use

FileHandle#moveTo(FileHandle dest)

It works fine in most cases. But when I try to rename, for example, a file "abc" to "ABC", the file gets deleted. I think the problem is that file names are insensitve ( at least on Desktop, Windows). This is the implementation of the method mentioned above (I left commentaries in the code):

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

Questions:

1) Is such behaviour intentional? Honestly, it feels wrong.

2) Is it ok to do renaming like this (it works in this case, but maybe there are another caveats):

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

If not, what's the right way?

Update

As @exenza suggested, opened an issue on LibGDX issue tracker

On Windows, file names are case insensitive. This means that "abc" and "ABC" refer to the same file. Your copyTo() call, will copy the file to itself. Then delete() deletes the file. During all of this there is only one file and no copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM