2014-01-06 34 views
0

我想将一个文件夹的所有内容复制到SDCard上的另一个文件夹。 我想在操作系统级别执行此操作。我试过使用以下命令: cp -a/source /。/dest/,这不起作用,它说权限被拒绝由于我的设备没有根。然而,一个有趣的事情是,它可以让我执行RM - R的源在OS级别将文件夹的内容复制到SD卡上的另一个文件夹?

String deleteCmd = "rm -r " + sourcePath; 
      Runtime delete_runtime = Runtime.getRuntime(); 
      try { 
       delete_runtime.exec(deleteCmd); 
      } catch (IOException e) { 
       Log.e("TAG", Log.getStackTraceString(e)); 
      } 

请告诉我,如果存在一种方法,通过它我可以在OS层面实现这一目标还有我的最后的手段将是这个LINK。 在此先感谢。

回答

1

经过研究更多我找到了适合我的要求的完美解决方案。该文件副本是TREMENDOUSLY FAST

mv命令为我实现了魔法,它将源文件夹内的所有文件移动到目标文件夹,并在复制后删除源文件夹。

String copyCmd = "mv " + sourcePath + " " + destinationPath; 
Runtime copy_runtime = Runtime.getRuntime(); 
try { 
     copy_runtime.exec(copyCmd); 
    } catch (IOException e) { 
     Log.d("TAG", Log.getStackTraceString(e)); 
    } 
+0

mv是不同的,然后复制,有其优点和缺点。 – skoperst

+0

@skoperst是的,我知道我的朋友,但正如我所说的“它适合我的要求”,所以它适合我。但是,如果我没有意识到它们的缺点,你可以善待它的缺点。 – CodeWarrior

-1
public void copyDirectory(File sourceLocation , File targetLocation) 
throws IOException { 

    if (sourceLocation.isDirectory()) { 
     if (!targetLocation.exists() && !targetLocation.mkdirs()) { 
      throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath()); 
     } 

     String[] children = sourceLocation.list(); 
     for (int i=0; i<children.length; i++) { 
      copyDirectory(new File(sourceLocation, children[i]), 
        new File(targetLocation, children[i])); 
     } 
    } else { 

     // make sure the directory we plan to store the recording in exists 
     File directory = targetLocation.getParentFile(); 
     if (directory != null && !directory.exists() && !directory.mkdirs()) { 
      throw new IOException("Cannot create dir " + directory.getAbsolutePath()); 
     } 

     InputStream in = new FileInputStream(sourceLocation); 
     OutputStream out = new FileOutputStream(targetLocation); 

     // Copy the bits from instream to outstream 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
    } 
} 
+0

这不会帮助配偶。看到我在我的问题结束时提供的LINK,它具有类似的实现,即时尝试寻找解决方法。 – CodeWarrior

0

你的错误是拒绝“权限”,要么你没有执行“CP”二进制许可或者您没有权限来创建其他的东西SD卡或很多可能出错目录。

使用adb shell了解更多关于cp命令的知识,它位于/ system/bin /中。

或者

您可以下载终端仿真器应用程序并尝试从外壳运行命令。

使用ls -l/system/bin检查权限。

除了所有这些,不要忘了你的SD卡有FAT文件系统,而cp -a使用chmod和utime的组合,这也可能超出你的权限范围。而且我不是在谈论如何在FAT上做chmod fs并不是一个好主意。除非你完全理解你在这里遇到的问题,否则我还会建议使用你提供的LINK。

+0

感谢回答队友,但我研究了一下,发现,因为Android使用的Linux内核是一个精简版和cp命令不包括在其中,是的,我已经试过它在亚行壳第一,然后只有我在这里提出这个问题。 – CodeWarrior

相关问题