2013-11-26 6 views
-1

안드로이드에 전체 디렉토리를 복사하려는 경우 onClick .. 어떻게해야합니까?어떻게 전체 디렉토리를 복사 할 수 있습니까?

String sdCard = Environment.getExternalStorageDirectory().toString(); 
File srcFolder = new File(sdCard +"tryFirstFolder"); 
File destFolder = new File(sdCard +"/TryFolder"); 

을 그리고 난이 사용하는 다른 destFolder

+0

모든 파일을 반복적으로 순환하여 파일을 복사하는 것입니다 (또는 파일이 아닌 폴더 인 경우 더 깊게 이동하는 것). –

답변

0

한 위치에서 디렉토리를 복사 할 수 있습니다에 srcFolder의 전체 내용을 복사하는 코드가 필요합니다 : 나는이 또한

public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation) 
     throws IOException { 

    if (sourceLocation.isDirectory()) { 
     if (!targetLocation.exists()) { 
      targetLocation.mkdir(); 
     } 

     String[] children = sourceLocation.list(); 
     for (int i = 0; i < sourceLocation.listFiles().length; i++) { 

      copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]), 
        new File(targetLocation, children[i])); 
     } 
    } else { 

     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(); 
    } 

} 

이 링크를 클릭하면 한 폴더에서 다른 폴더로 파일을 복사하거나 이동할 수 있습니다. http://www.codeofaninja.com/2013/04/copy-or-move-file-from-one-directory-to.html

+0

하지만이 코드를 사용하여 sourceLocation 및 targetLocation 중 어느 것을 결정할 수 있습니까 ?? 어떻게 onClick에서이 메서드를 호출 할 수 있습니까? –

관련 문제