天天看點

Android--檔案或目錄拷貝、複制、粘貼

版權聲明:本文為部落客原創文章,轉載請标明出處。 https://blog.csdn.net/chaoyu168/article/details/53762886

需要給 AndroidManifest.xml裡加上權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>           

源碼:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class Copy_File extends Activity
{
    private Button m_btn = null;
    private final static String FROMPATH = "/mnt/sdcard/A/";
    private final static String TOPATH = "/mnt/sdcard/B/";
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_btn = (Button)findViewById(R.id.button1);
        m_btn.setOnClickListener(new OnClickListener()
        {
 
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                if(copy(FROMPATH, TOPATH)==0)
                {
                    Toast.makeText(Copy_File.this,"檔案拷貝成功!!!",20000).show();
                }else
                {
                    Toast.makeText(Copy_File.this,"檔案拷貝失敗!!!",20000).show();
                }
            }
                 
        });
    }
     
    public int copy(String fromFile, String toFile)
    {
        //要複制的檔案目錄
        File[] currentFiles;
        File root = new File(fromFile);
        //如同判斷SD卡是否存在或者檔案是否存在
        //如果不存在則 return出去
        if(!root.exists())
        {
            return -1;
        }
        //如果存在則擷取目前目錄下的全部檔案 填充數組
        currentFiles = root.listFiles();
         
        //目标目錄
        File targetDir = new File(toFile);
        //建立目錄
        if(!targetDir.exists())
        {
            targetDir.mkdirs();
        }
        //周遊要複制該目錄下的全部檔案
        for(int i= 0;i<currentFiles.length;i++)
        {
            if(currentFiles[i].isDirectory())//如果目前項為子目錄 進行遞歸
            {
                copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");
                 
            }else//如果目前項為檔案則進行檔案拷貝
            {
                CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
            }
        }
        return 0;
    }
     
   
    //檔案拷貝
    //要複制的目錄下的所有非子目錄(檔案夾)檔案拷貝
    public int CopySdcardFile(String fromFile, String toFile)
    {
         
        try
        {
            InputStream fosfrom = new FileInputStream(fromFile);
            OutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0)
            {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return 0;
             
        } catch (Exception ex)
        {
            return -1;
        }
    }
     
 
}           

繼續閱讀