zoukankan      html  css  js  c++  java
  • 大文件流操作及编码

    (一)大文件流操作
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace 文件流
    {
        class Program
        {
            static void Main(string[] args)
            {



                //string str = @"C:\Documents and Settings\steve\桌面\net1017\net基础加强\案例素材\1.rmvb";
                //byte[] byts = File.ReadAllBytes(str);
                //Console.ReadKey();

                //将1.rmvb 拷贝到 d:\1.rmvb
                string source = @"C:\Documents and Settings\steve\桌面\net1017\net基础加强\案例素材\1.rmvb";
                string target = @"d:\1.rmvb";
                CopyFile(source, target);
                Console.WriteLine("ok");
                Console.ReadKey();

            }
            static void CopyFile(string sourc, string target)
            {
                #region MyRegion
                // // 创建一个文件流
                // FileStream fsRead = new FileStream(sourc, FileMode.Open);



                // //
                // //
                //// fsRead.Flush();//将缓冲区的数据写入到流中。
                // //文件流使用完成以后必须关闭
                // fsRead.Close();
                // //用来释放一些非托管资源的。
                // fsRead.Dispose();

                //using (FileStream fsRead = new FileStream(sourc, FileMode.Open))
                //{
                //    //在using的小括号中包含的对象,当出了using的作用域范围以后
                //    //会自动调用该对象的Dispose()方法。
                //    //对于文件流调用Dispose()方法的时候会自动调用close()方法。
                //    //fsRead.Dispose();   
                //}
                //...

                //using (Person p = new Person())
                //{

                //}
                //Console.ReadKey();
                #endregion

                //创建一个读取文件的文件流
                FileStream fsRead = new FileStream(sourc, FileMode.Open);

                // 创建一个写入文件流
                FileStream fsWrite = new FileStream(target, FileMode.Create);
                using (fsRead)
                {
                    using (fsWrite)
                    {
                        long sum = fsRead.Length;
                        double readByte = 0;
                        //每次从文件中读取5MB,到内存中。
                        byte[] byts = new byte[1024 * 1024 * 5];
                        while (true)
                        {


                            //这个b就表示本次实际读取到的字节数。
                            //参数1:要将数据存放到的数组中。
                            //参数2:这个数组从第几个元素开始存储
                            //参数3:本次读取最多能读取多少个字节。
                            int b = fsRead.Read(byts, 0, byts.Length);
                            readByte += b;
                            // 如果本次没有读取到任何字节,则认为已经读取完毕,跳出循环
                            if (b <= 0)
                            {
                                break;
                            }
                            //fsWrite.Position

                            //参数1:要将那个数组中的数据写入文件
                            //参数2:从这个数组的第几个元素开始写
                            //参数3:本次要写的字节数(一般为上次实际读取的字节数)
                            fsWrite.Write(byts, 0, b);
                            //Console.Write(".");
                            Console.WriteLine((readByte / sum) * 100 + "%");

                        }


                    }
                }






            }
        }

        public class Person : IDisposable
        {
            public string Name
            {
                get;
                set;
            }
            public int Age
            {
                get;
                set;
            }
            public string Email
            {
                get;
                set;
            }


            #region IDisposable 成员

            public void Dispose()
            {
                Console.WriteLine("......................");
            }

            #endregion
        }
    }
    (二)文件编码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace _02_文件操作
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //选择文件
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.Filter = "文本文件|*.txt|所有文件|*.*";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    textBox1.Text = ofd.FileName;
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                //文件流的方式 读取文件

                using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open))
                {
                    byte[] buffer = new byte[1024*1024*5];
                    //把流中的内容读取到字节数组中
                    int length = fs.Read(buffer, 0, buffer.Length);

                    //字节数组转换成字符串
                   textBox2.Text =  System.Text.Encoding.GetEncoding("gbk").GetString(buffer);
                }

               
            }

            //选择要保存文件的路径
            private void button4_Click(object sender, EventArgs e)
            {
                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Filter = "文本文件|*.txt";

                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    textBox3.Text = sfd.FileName;
                }
            }

            private void button3_Click(object sender, EventArgs e)
            {
                //写入文件
                using(FileStream fs = new FileStream(textBox3.Text,FileMode.Create))
                {
                    //字符串转换成字节数组
                    byte[] buffer = System.Text.Encoding.GetEncoding("gbk").GetBytes(textBox4.Text);
                    //把字节数组通过文件流写入文件
                    fs.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }

  • 相关阅读:
    Code Forces 650 C Table Compression(并查集)
    Code Forces 645B Mischievous Mess Makers
    POJ 3735 Training little cats(矩阵快速幂)
    POJ 3233 Matrix Power Series(矩阵快速幂)
    PAT 1026 Table Tennis (30)
    ZOJ 3609 Modular Inverse
    Java实现 LeetCode 746 使用最小花费爬楼梯(递推)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2640586.html
Copyright © 2011-2022 走看看