• 大文件Copy 大数据量复制


     private void button3_Click(object sender, EventArgs e)
            {
                
                Thread thread = null;
                //为了不让界面死掉,要将该操作放在一个线程中
                thread = new Thread
                    (
                    () => 
                    {
                        //告诉系统不去检测非法的跨线程调用 
                        CheckForIllegalCrossThreadCalls = false;
                        
                        //创建一个文件流指向源文件
                        FileStream fsRead = new FileStream(this.textBox1.Text, FileMode.Open);
                        //创建一个文件流指向目标文件
                        FileStream fsWrite = new FileStream(this.textBox2.Text, FileMode.Create);
                        //记录一下该文件的长度
                        long fileLength = fsRead.Length;
                        //定义一个1M的缓冲区
                        byte[] buffer = new byte[1024 * 1024];
                        //先读取一次,并且将读取到的真正内容长度记录下来
                        int readLength = fsRead.Read(buffer, 0, buffer.Length);
                        //用来记录已经将多少内容写入到了文件中
                        long readCount = 0;
                        //只要读取到的内容不为0就接着读
                        while (readLength!=0)
                        {
                            //将前面已经读取到内存中的数据写入到文件中
                            fsWrite.Write(buffer ,0,readLength);
                            //已经读取的数量累加
                            readCount += readLength;
                            //计算已经读取的数据百分比
                            int percentage =(int)( readCount * 100 / fileLength);
                            this.progressBar1.Value = percentage;
                            //进行下一次读取
                            readLength = fsRead.Read(buffer, 0, buffer.Length);

                        }
                        fsRead.Close();
                        fsWrite.Close();
                        //清空缓冲区
                        buffer = null;
                        //回收一下内存
                        GC.Collect();
                        thread.Abort();
                        
                    }
                    );
                thread.Start();
            }

  • 相关阅读:
    冲销收货
    收货MIGO
    删除PO
    创建po
    File的功能--> 获取功能-->所有的根目录 | 创建文件功能,但是如果文件已经存在-->不再创建(新手)
    在d盘创建文件夹,里面有aaa.txt/bbb.txt/ccc.txt,然后遍历出aaa文件夹下的文件(新手)
    创建一个唱歌类集合,创建一个电影类集合,让其归类打印(新手)
    抛出异常(新手)
    Map-->HashMap练习(新手)
    JAVA-迭代器增强型for循环。(新手)
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3293504.html
Copyright © 2020-2023  润新知