zoukankan      html  css  js  c++  java
  • MemoryStream和FileStream

    一,FileStream对象的数据来自文件,而MemoryStream对象的数据来自内存缓冲区。这两个类都继承自Stream类。

    二,抽象基类System.IO.Stream代表流,它提供Read和Write两个方法。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MemoryStream mStream = new MemoryStream();
    
            BufferedStream bStream = new BufferedStream(mStream);
    
            byte[] b = new byte[10];
    
            for (int i = 0; i < 10; i++)
            {
    
                bStream.WriteByte((byte)i);     //将一个字节写入缓冲流的当前位置。
    
            }
    
            bStream.Position = 0;
    
            bStream.Read(b, 0, 10);            //将字节复制到的缓冲区,从0处开始读取字节,要读取的10字节数。
    
            for (int i = 0; i < 10; i++)
            {
    
                Label1.Text = Label1.Text + b[i];
            }
    
            Label2.Text = bStream.ReadByte().ToString();  // 转换为 int 的字节,或者如果从流的末尾读取则为 -1,则-1即是结束
        }
    }

     二,文件流获取操作

           string path2 = Server.MapPath("~/12.txt");
    
            FileStream fileStream = File.OpenRead(path2);  // 打开文件获取流
    
            Stream stream = fileStream;
    
            fileStream.Close();
  • 相关阅读:
    希腊字母写法
    The ASP.NET MVC request processing line
    lambda aggregation
    UVA 10763 Foreign Exchange
    UVA 10624 Super Number
    UVA 10041 Vito's Family
    UVA 10340 All in All
    UVA 10026 Shoemaker's Problem
    HDU 3683 Gomoku
    UVA 11210 Chinese Mahjong
  • 原文地址:https://www.cnblogs.com/May-day/p/6134602.html
Copyright © 2011-2022 走看看