0
\$\begingroup\$

I'm using this line of code to delete a file:

if(Gdx.files.local("/files/ids.json").exists()) {
   Gdx.files.local("/files/ids.json").delete();
}

The same code works for all previous levels except the last one. The file does exist, but the delete() method returns false.

Any help would be greatly appreciated as it has exhausted me. I've seen similar topics in Java but nothing has worked for me. It's really weird that the same code works in some cases.

Edit

Also, in case this provides any information, when I step through the program with the debugger, the delete() returns true.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You are missing a bracket there in if statement

Gdx.files.local("/files/ids.json").exists())

I wonder how that code got compiled in the first place

If you just missed it while writing the post, then make sure that file is not used somewhere else when you are trying to delete it.

There is that scenario when delete() method returns false:

the delete method looks like this:

public boolean delete () {
    if (type == FileType.Classpath) throw new GdxRuntimeException("Cannot delete a classpath file: " + file);
    if (type == FileType.Internal) throw new GdxRuntimeException("Cannot delete an internal file: " + file);
    return file().delete();
}

and the delete from file.delete() looks like this :

public boolean delete() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkDelete(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.delete(this);
}

You can notice the isInvalid() method which is checking for a NULL character in the path (basicly checks if the the path is invalid) :

/**
 * Check if the file has an invalid path. Currently, the inspection of
 * a file path is very limited, and it only covers Nul character check.
 * Returning true means the path is definitely invalid/garbage. But
 * returning false does not guarantee that the path is valid.
 *
 * @return true if the file path is invalid.
 */
final boolean isInvalid() {
    if (status == null) {
        status = (this.path.indexOf('\u0000') < 0) ? PathStatus.CHECKED
                                                   : PathStatus.INVALID;
    }
    return status == PathStatus.INVALID;
}

Im mentioning that because i have been in situations when i used strings that contained weird unicode chars in it and even thou I couldn't see it normally it was there and IntelliJ was the root cause (what came out much later). Make sure that the path you specified is correct, try to invalidate the files, clean and rebuild the project.

\$\endgroup\$
3
  • \$\begingroup\$ The parenthesis was just a typo when copying it here. I'll have a look at what you mention. The thing is though, that the same exact line of code (not just the same copy of code, but the same specific line) is used in all cases, but doesn't work in one of them. Also, when I step through the program with the debugger, the delete() returns true; \$\endgroup\$
    – xmac
    Commented Aug 12, 2019 at 10:32
  • \$\begingroup\$ It would mean that the file is still locked when delete attempt is executed and when you use debugger you kinda slows it down so any process before deleting has been completed and file is unlocked \$\endgroup\$
    – Jarek
    Commented Aug 12, 2019 at 11:06
  • \$\begingroup\$ So could it cause any problems if I put the Thread to sleep for a bit before I call delete()? It seems to work, although I'm not really happy with it as a solution. \$\endgroup\$
    – xmac
    Commented Aug 12, 2019 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .