复制文件夹及显示进度条

package com.lzw.rwtest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
    private File sourceFile;
    private File newFile;
    private File logFile;
    private BufferedWriter lWriter;
    private ProgressDialog myProgressDialog;
    long size;
    private TextView tv;
    private Handler handler;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.tv1);
        logFile=new File("/flash/R&Wlog.txt");
         sourceFile=new File("/flash/123.rmvb");
         newFile=new File("/sdcard/123.rmvb");
         try
        {
            lWriter=new BufferedWriter(new FileWriter(logFile, true));
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         //获得文件的大小
         size=sourceFile.length();
         if (checkSDcard()==true)
        {
             moveFiletosd();
        }
         else 
         {
             writelog("SD卡不存在,测试停止,测试失败");
        }



            handler=new Handler()
            {
                public void handleMessage(Message msg)
                {
                    switch (msg.what)
                    {
                    case 1:
                        //计算复制进度
                        int i=(int)((100*newFile.length()/size));
                        //更新进度条
                        myProgressDialog.setProgress(i);
                        //复制完毕,关闭进度条
                        if (myProgressDialog.getProgress()>=100)
                        {
                            //关闭进度条
                            myProgressDialog.dismiss();
                            //删除源文件
                            sourceFile.delete();
                            //检查文件
                            check(newFile);
                            //flash->sdcard完成,开始sdcard->flash
                            moveFiletoflash();
                        }
                        break;
                    case 2:
                        //计算复制进度
                        int j=(int)((100*sourceFile.length()/size));
                        //更新进度条
                        myProgressDialog.setProgress(j);
                        //复制完毕,关闭进度条
                        if (myProgressDialog.getProgress()>=100)
                        {
                            myProgressDialog.dismiss();
                            newFile.delete();
                            check(sourceFile);
                            tv.setText("测试完成");
                            try
                            {
                                 //log写入完成后关闭输入流
                                lWriter.close();
                            } catch (IOException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        break;
                    }
                    super.handleMessage(msg);
                }


            };

    }

    //flash移动文件到sdcard
    public void moveFiletosd()
    {
        if (sourceFile.exists())
        {
            tv.setText("由flash向sd卡复制文件....");
                showDialog(0);
        }
        else {
            //源文件不存在
            tv.setText("flash中没有源文件!(\"flash/123.rmvb\")");
            writelog("Error:flsah中找不到源文件,请检查源文件是否存在!");
        }
    }


    //sd卡移动文件到flash
    public void moveFiletoflash()
    {
        if (newFile.exists())
        {
            tv.setText("由sd卡向flash复制文件....");
                showDialog(1);
        }
        else {
            //源文件不存在
            tv.setText("sdcard中没有源文件!");
            writelog("Error:SDcard中找不到源文件,请检查源文件是否存在!");
        }

    }


    //复制文件
    public void copyfile(File oldFile,File newfile,int id)
    {
         try
            {
                FileInputStream fis=new FileInputStream(oldFile);
                FileOutputStream fos=new FileOutputStream(newfile);
                byte[] buf=new byte[1024*1024];
                int readline=0;
                while ((readline=fis.read(buf))!=-1)
                {
                    fos.write(buf, 0, readline);
                    handler.sendEmptyMessage(id);
                }
                fos.flush();
                fos.close();
                fis.close();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    //判断复制后文件是否存在,并判断大小是否一致
    public void check(File file)
    {
        if (file.exists())
        {
            long newsize=file.length();
            if (newsize==size)
            {
                //文件复制成功
                Toast.makeText(MainActivity.this, "复制文件成功", Toast.LENGTH_SHORT).show();
            writelog("复制文件成功");
            }
            else {
                //大小不一致 复制失败
                writelog("Error:文件大小与原文件不符");
            }
        }
        else {
            //复制文件失败
            writelog("复制文件失败");
        }
    }
    //判断SD卡是否存在
    public boolean checkSDcard()
    {
        String status=Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED))
        {
            return true;
        }
        else 
        {
            return false;
        }
    }

    //创建progressdialog
    protected Dialog onCreateDialog(int id)
    {
        myProgressDialog=new ProgressDialog(MainActivity.this);
        myProgressDialog.setMax(100);
        myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        myProgressDialog.setTitle("正在复制文件");
        myProgressDialog.setCancelable(false);
        switch (id)
        {
        case 0:

            myProgressDialog.setMessage("源文件:"+sourceFile.getPath()+"\n目标文件:"+newFile.getPath());
            break;
        case 1:

            myProgressDialog.setMessage("源文件:"+newFile.getPath()+"\n目标文件:"+sourceFile.getPath());
            break;
        default:
            break;
        }
        return myProgressDialog;
    }

    protected void onPrepareDialog(final int id, Dialog dialog)
    {
        // TODO Auto-generated method stub
        super.onPrepareDialog(id, dialog);
        switch (id)
        {
        case 0:
            //线程处理
            new Thread()
            {
                public void run()
                {
                    copyfile(sourceFile,newFile,1);
                }
            }.start();
            break;
        case 1:
            new Thread(){
                public void run()
                {
                    copyfile(newFile,sourceFile,2);
                }
            }.start();
            break;
        }


    }
    //写入log
    public void writelog(String string)
    {
        try
        {
            Date nowtimeDate=new Date(System.currentTimeMillis());
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String str=format.format(nowtimeDate);
            lWriter.write(str+"  "+string);
            lWriter.newLine();
            lWriter.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值