Home > Backend Development > C#.Net Tutorial > .net stream - a brief introduction to the stream type system

.net stream - a brief introduction to the stream type system

黄舟
Release: 2017-02-24 10:42:38
Original
1583 people have browsed it


Type system of streams

##Data transmissionOperations on file streams become simpler


Basic flow

##Basic flow

Decorator flow

Wrapper class

Help class

Inherits from basic streamstream

Function added on the basicStream

##

It still feels quite Java-like.


Decorator flow

## Stream

The corresponding backing storage is file

Memory

Network resource

FileStream

MemoryStream

NetWorkStream

IsolatedStorgaeFileStream:

Inherits from FileStream

##System.net.security##Enhanced Caching

The Lenovo Decorator pattern is estimated to add some other functions to the basic stream, which is a bit like an extension of the functions of the basic type.

Wrapper class

Introduces streams for data transmission;

##BufferdStream

DeflateStream

GZipStream

CryptoStream

AuthenticatedStream

System.io

System.IO.Compression

System.IO.Compression

System .Security.Cryptography

Compression

Decompression

Encryption and Decryption

Security

 #region StringReader,StringWriter的使用;

//                string text = @"姓名:水田如雅;
//                                年龄:20;
//                                性别:女";
//                StringReader reader = new StringReader(text);
//                int c = reader.Read();
//                Console.WriteLine((char)c);

//                char[] buffer = new char[7];
//                reader.Read(buffer, 0, buffer.Length);
//                Console.WriteLine(string.Join("", buffer));

//                string line = reader.ReadLine();
//                Console.Write(line);

//                string rest = reader.ReadToEnd();
//                Console.WriteLine(rest);

//                reader.Dispose();
//                Console.ReadKey();

            #endregion

            #region streamreader/StreamWriter的使用

                //FileStream fs = new FileStream("AboutVac.txt", FileMode.Open, FileAccess.Read);
                //StreamReader reader=new StreamReader(fs,Encoding.GetEncoding("GB2312"));

                //do
                //{
                //    Console.WriteLine(reader.ReadLine());

                //} while (reader.Read()>0);

                //StreamWriter writer = new StreamWriter("aboutMe.txt", false, Encoding.UTF8);
                //writer.Write("hello,word");
              
                //reader.Dispose();
                //writer.Dispose();

                //Console.ReadKey();


            #endregion

            #region BinaryReader/BinaryWriter的使用

                //Product product = new Product("Product.txt") { 
            
                //        id=110,
                //        price=123.3,
                //        Name="lhc"
            
                //};
                //product.Save();
                //Product newItem =new  Product("Product.txt");
                //newItem.Load();
                //Console.WriteLine(newItem);
                //Console.ReadKey();
                


            #endregion
Copy after login
public class Product {

        public int id { get; set; }
        public string Name { get; set; }
        public double price { get; set; }

        private string filePath;
        public Product(string filePath) { this.filePath = filePath; }

        public void Save() {

            FileStream fs = new FileStream(this.filePath, FileMode.Create, FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(fs);
            writer.Write(this.id);
            writer.Write(this.Name);
            writer.Write(this.price);
            writer.Dispose();
        }

        public void Load() {

            FileStream fs = new FileStream(this.filePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(fs);
            this.id = reader.ReadInt32();
            this.Name = reader.ReadString();
            this.price = reader.ReadDouble();
            reader.Dispose();
        
        }

        public override string ToString()
        {
            return string.Format("ID:{0},Name:{1},Price:{2}", this.id, this.Name, this.price);

        }
    }
Copy after login

Help class

Namespace:system.io

##TextWriter

TextReader(defined set of Universal way to read and write character data)

StreamReader

StreamWriter

##1

, inherited from TextWriter

Text

Reader defines a set of common methods for reading and writing character data

2, suitable for reading and writing text characters

BinaryReader

: used to read binary data into the stream Way to write primitive types

BinaryWriter

: Used to read primitive types from streams

St

ringReaderStringWriter:

1, also inherited from

TextWriter

Text

Reader, used to process strings

Create;Processing path
##File

FileInfo

Path

##Directory

/DirecoryInfo

Open;

openRead;

openWrite;

Copy;


##

  #region 工具类示例
               // File.WriteAllText("FileTest.txt", "hello,i'm using file ", Encoding.UTF8);
  #endregion
Copy after login


When using it, you should first consider whether the existing high-level classes encapsulate the available methods. If not, then consider using basic classes for encapsulation.


The above is a brief introduction to .net flow - the flow type system. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!











Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template