2012-03-07 59 views
6

Tôi đang làm việc trên Android. Yêu cầu của tôi là tôi có một thư mục với một số tệp, sau đó tôi đã tải xuống một số tệp khác vào một thư mục khác và ý định của tôi là sao chép tất cả các tệp từ thư mục mới nhất vào thư mục đầu tiên. Và trước khi sao chép các tập tin vào thư mục đầu tiên từ mới nhất tôi cần phải loại bỏ tất cả các tập tin từ thư mục đầu tiên.Làm cách nào để sao chép các tệp của tôi từ một thư mục này sang một thư mục khác?

+0

cũng đôi khi tìm kiếm các android/java doc hoặc ít nhất là sử dụng "tìm kiếm" hộp có thể được thực sự thực sự hữu ích – Blackbelt

+0

Bạn đã tìm thấy một giải pháp? Xin cho biết? – marienke

Trả lời

21
void copyFile(File src, File dst) throws IOException { 
     FileChannel inChannel = new FileInputStream(src).getChannel(); 
     FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
     try { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } finally { 
      if (inChannel != null) 
      inChannel.close(); 
      if (outChannel != null) 
      outChannel.close(); 
     } 
    } 

Tôi không thể nhớ nơi tôi tìm thấy điều này, nhưng nó là từ một bài viết hữu ích mà tôi đã sử dụng để sao lưu cơ sở dữ liệu SQLite.

+0

Đơn giản và hoàn hảo. Không biết nếu sẽ hoạt động tốt với các tập tin lớn, chỉ trong trường hợp, hãy để kiểm tra nó. – jfcogato

0

Bạn phải sử dụng cũng dưới mã:

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

} 
+0

lưu tệp thành địa phương thành công –

+0

Vui lòng cố gắng hạn chế txtspk tại đây. Tìm kiếm nhanh cho biết bạn đã sử dụng "u" có nghĩa là "bạn" 39 lần và "ur" có nghĩa là "của bạn" 25 lần. Đó là rất nhiều công việc sửa chữa bạn đang tạo cho tình nguyện viên. – halfer

5

Apache FileUtils thực hiện điều này rất đơn giản và độc đáo ..

bao gồm commons Apache io gói thêm commons-io.jar

hoặc

commons-io android gradle dependancy

compile 'commons-io:commons-io:2.4' 

Thêm mã này

String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/sourceFile.3gp"; 
     File source = new File(sourcePath); 

     String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/destFile.3gp"; 
     File destination = new File(destinationPath); 
     try 
     { 
      FileUtils.copyFile(source, destination); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
Các vấn đề liên quan