noteBook2.13-C#基础第十三天

FileStream文件流(操作对象为字节,适用于所有文件)

StreamReader&StreamWriter:操作对象为字符

//

FileStream fsRead=new FileStream(@"  ",FileMode.OpenOrCreat,File.Access.Read);//内容分别为:路径,对文件的操作选择,对文件内数据的操作选择

byte[] buffer=new byte[1024*1024*5];//创建缓存为5M

int r=fsRead.Read(buffer,0,buffer.Length);//每次向buffer内读取Length长度的数据,从0开始,返回值r为本次实际读取到的有效字节数

//下面解码

String s=Encoding.Default.GetString(buffer,0,r);//解码范围是:0—r

fsRead.Close();//关闭流

fsRead.Dispise();//释放占用的资源

**如果使用using,则可以省去关闭流和释放资源的步骤,会自动完成。

//

Using(FileStream fsWrite=new FileStream(@"      ",FileMode.OpenOrCreate,FileAccess.Write))

{

       string str="男";

       byte[] buffer=Encoding.Default.GetBytes(str);

       fsWrite.Write(buffer,0,buffer.Length);

}

//这里会覆盖相同大小的已有内容,将File.OpenOrCreate改为File.Append则为追加,不会覆盖

使用FileStream实现多媒体文件的复制

//先读,再写。

String source=@"       ";//源路径

String target=@" ";//复制路径

CopyFile(source,target);//调用自写函数

Public Static void CopyFile(String source, String target)

{using (FileStream fsRead=newFileStream(source,FileMode.Open,FileAccess.Read))

       //1.创建了负责读取的流;2.下面创建负责写入的流

       {

              using(FileStreamfsWrite=new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write))

              {

                     byte[]buffer=new byte[1024*1024*5];

                     while(true)

                     {

                            intr=fsRead.Read(buffer,0,buffer.Length);

                            if(r==0)

                            {

                                   break;

                            }

                            fsWrite.Write(buffer,0,r);

                     }

              }

       }

}

StreamReader&StreamWriter

//

using(StreamReader sr=new StreamReader(@"   "))

{

       while(!sr.EndOfStream)

       {

              Console.WriteLine(sr.ReadLine());//持续读取,直到读到流尾

       }

}

using(StreamWriter sw=new StreamWriter(@"    "),true)//加true表示写入是不覆盖

{

       sw.Write("男");

}

//

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace NoteBook { /// <summary> /// About 的摘要说明。 /// </summary> public class About : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public About() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(About)); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.ForeColor = System.Drawing.Color.Red; this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image"))); this.label1.Location = new System.Drawing.Point(0, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(496, 128); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // label2 // this.label2.Font = new System.Drawing.Font("华文新魏", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label2.Location = new System.Drawing.Point(16, 147); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(192, 32); this.label2.TabIndex = 1; this.label2.Text = "记事本编辑器"; // // label3 // this.label3.Font = new System.Drawing.Font("华文行楷", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label3.ForeColor = System.Drawing.Color.RoyalBlue; this.label3.Location = new System.Drawing.Point(16, 224); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(232, 24); this.label3.TabIndex = 2; this.label3.Text = " 版权所有,翻版必究!"; // // label4 // this.label4.Font = new System.Drawing.Font("华文新魏", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label4.Location = new System.Drawing.Point(240, 151); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(248, 32); this.label4.TabIndex = 3; this.label4.Text = "计算机3063班 C#课程设计 "; // // label5 // this.label5.Image = ((System.Drawing.Image)(resources.GetObject("label5.Image"))); this.label5.Location = new System.Drawing.Point(256, 197); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(240, 64); this.label5.TabIndex = 4; // // About // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.Black; this.ClientSize = new System.Drawing.Size(496, 262); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.ForeColor = System.Drawing.Color.Red; this.Name = "About"; this.Text = "关于我的记事本"; this.ResumeLayout(false); } #endregion } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值