Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to move a directory in Android?

Tags:

android

What is the fastest way to move a directory in Android? In most, but not all cases, the source and destination are located on the same sdcard file system.

Current, my code goes through the entire directory structure, and copy the content of each file into a new file with the same name in the new location. It then verifies the file size matches, then deletes the source file.

For each file, I current run (with additional exception handling):

    try{
      source = new FileInputStream(fileFrom).getChannel();
      destination = new FileOutputStream(fileTo).getChannel();
      destination.transferFrom(source, 0, source.size());
    } finally {
      source.close();
      destination.close();
    }

This seems to be slow as well as way to much computational work for what I expect could maybe be a simple instant "node modification" at the raw file system level.

like image 790
starvingmind Avatar asked Feb 20 '23 17:02

starvingmind


1 Answers

As long as Files are on the same filesystem you can actually move them File#renameTo(File).

if (!fileFrom.renameTo(fileTo)) {
    copy(fileFrom, fileTo);
    // delete(fileFrom);
}
like image 137
zapl Avatar answered Mar 08 '23 08:03

zapl