I think the issue is that:
File f = adapter.getItem(i);
Gives use some File f
, say where f
cooresponds to say: user2351234/Desktop
. Then, you do:
File file = new File(f.getAbsolutePath(), "helloworld");
Which says to make a File file
, where file
cooresponds to: user2351234/Desktop/helloworld
. Next, you call:
f.renameTo(file)
which attempts to rename f
, user2351234/Desktop
to user2351234/Desktop/helloworld
, which doesn't make sense since in order for user2351234/Desktop/helloworld
to exist, user2351234/Desktop
would have to exist, but by virtue of the operation it would no longer exist.
My hypothesis may not be the reason why, but from Why doesn't File.renameTo(…) create sub-directories of destination?, apparently renameTo
will return false
if the sub-directory does not exist.
If you want to just change the name of the file, do this:
File f = adapter.getItem(i);
String file = f.getAbsolutePath() + "helloworld";
f = new File(file);
EDIT:
My proposed solution should work, but if my hypothesis about why your way does not work is incorrect, you may want to see this answer from Reliable File.renameTo() alternative on Windows?