有关于一些常见的流用法

来源:互联网 发布:德国男生眼中美女知乎 编辑:程序博客网 时间:2024/04/30 01:18

基于接口编程

ProviderName的用途是区别是用哪个数据库

有关处理文件时的东西一定要注意编码问题

写一些函数类首先考虑能不能抽象成接口或抽象类或虚方法等

识别对象

窗体就是一个类

索引会编译成方法

Virtual 调用子类的方法,如果子类的没有实现

才回来调用父类的

窗体复用的用vritual

继承父窗体override重写

接口可以多继承

抽象类只能单继承

模板方法模式案例

就是把父类的东西继承并且重写实现父类的virtual

 

装饰者模式

把一个类写到另一个类的成员

包装一下

这就是装饰者模式

把一个类放到另一个类中装饰

但是还是实现成同一个接口

 

 

        拷贝文件的两种方式:将源文件内容全部读到内存中,再写到目标文件中;读取源文件的1KB内存,写到目标文件中,再读取源文件的1KB内存,再写到目标文件中……。第二种方式就是一种流(Stream)的操作。

        File.ReadAllTextFile.WriteAllText进行文件读写是一次性读、写,如果文件非常大会占内存、慢。需要读一行处理一行的机制,这就是流(Stream)Stream会只读取要求的位置、长度的内容。

        就像Sqldatareader一样,Stream不会将所有内容一次性读取到内存中,有一个指针,指针指到哪里才能读、写到哪里。

        流有很多种类,文件流是其中一种。FileStreamnew FileStream(c:/a.txt, filemode, fileaccess)后两个参数可选值及含义自己看。 FileStream可读可写。可以使用File.OpenReadFile.OpenWrite这两个简化调用方法。

        练习:文件加密(每一位用255-r

        byte[]是任何数据的最根本表示形式,任何数据最终都是二进制。

        Write用于向当前位置写入若干字节,Read用户读取若干字节。(*)每次ReadWrite指针都会自动后移。

        文件流的FlushCloseDispose

 

 

读一点写一点

拷文件都是流,流的处理

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 文件的加密解密

{

    public partial class Form1 : Form

    {

        publicForm1()

        {

            InitializeComponent();

        }

 

        privatevoid btnAdd_Click(objectsender,EventArgs e)

        {

 

            FileInfofileinfo =new FileInfo(txtSourse.Text);

           progressBar1.Maximum= (int)fileinfo.Length;

          

            using(FileStream outFile=newFileStream(txtTarget.Text,FileMode.Create))

            {

                using(FileStream sourseFile=newFileStream (txtSourse.Text,FileMode.Open))

                {

                    byte[]bytes=new byte[1024*1024*4];

                    intreadCount=0;

                    while((readCount=sourseFile.Read(bytes,0,bytes.Length))>0)

                    {

                        for (int i = 0; i < readCount;i++)

                        {

                            bytes [i]= (byte)(byte.MaxValue -bytes[i]);

                        }

                        outFile.Write(bytes, 0,readCount);

                        progressBar1.Value +=readCount;

 

                    }

                 

 

                }

            }

            progressBar1.Value = 0;

        }

    }

}

 

 

 

 

 

 

 

 

StreamReader streamwrite是纯读取文本

有的stream子类不支持指针的后退

HssFworkbook要求一个指针能随意移动的流

当流不支持向后移动可以用内存流

做文件下载用流操作

尤其是内存流

 

压缩和解压

在这个项目中遇到的问题是压缩只能是写的不能是读的,解压是读的,所以你必须

先从文件中读出数据到缓存区中,在压缩到文件中。这有个中转站(缓存区)

 

解压的时候是读出来直接可以写入文件

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;

using System.IO.Compression;

 

namespace 流操作

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        privatevoid button1_Click(objectsender,EventArgs e)

        {

            using(FileStream filestream =new FileStream(@"c:\p.txt",FileMode.Open,FileAccess.Read))

            {

                using(FileStream outstream =new FileStream(@"c:\a.txt",FileMode.Create,FileAccess.Write))

                {

                    using(GZipStream zipstream =new GZipStream(filestream,CompressionMode.Decompress))

                    {

                        int readercount = 0;

                        byte[] bytes=new byte[1024*1024*4];

                        while ((readercount=zipstream.Read(bytes,0,bytes.Length))>0)

                        {

                            outstream.Write(bytes,0, readercount);

                         

                        }

                    }

                }

            }

            MessageBox.Show("解压完毕");

 

 

            }

 

        privatevoid button2_Click(objectsender,EventArgs e)

        {

            Streaminstream = File.OpenRead(@"c:\d.txt");

            byte[]bytes=new byte[1024*1024*4];

            intcount = 0;

            Streamstream = File.OpenWrite(@"c:\p.txt");

            GZipStreamzip = new GZipStream(stream,CompressionMode.Compress);

            while((count=instream.Read(bytes,0,bytes.Length))>0)

            {

 

               

                zip.Write(bytes, 0, count);

             

               

            }

            zip.Dispose();

            stream.Dispose();

        

          

           

        }

           

           

    }

      

   

}

 

 

 

 

StreamRead streamWriter

 

            StreamReaderreader =new StreamReader(@"c:\d.txt",Encoding.Default);

            char[]buffer=newchar[1024*1024*4];

            intreadercount = 0;

            StreamWriterwriter =new StreamWriter(@"c:o.txt");

            while((readercount = reader.Read(buffer, 0, buffer.Length)) > 0)

            {

                writer.Write(buffer);

               

            }

         

序列化和反序列化

BinaryFormatter类的重要性

一个类必须标记[serializable]标记可序列化

 

注意啦!

当把一个类序列化时必须是与它关联的类也必须

序列化

 

Dat是个二进制文件

可以序列化你的文件内容再存入进去
原创粉丝点击