2014-01-06 35 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

相關問題