logo search
CSharp_Prog_Guide

Классы, используемые для чтения и записи в поток

Классы BinaryReader и BinaryWriter производят чтение и запись кодированных строк и простых типов данных в потоки.

Класс StreamReader считывает символы из потоков, используя Encoding для преобразования символов в байты, и наоборот. StreamReader имеет конструктор, который пытается выяснить подходящую кодировку для заданного потока, на основе наличия специфичной для кодировки преамбулы, такой как метка порядка байтов.

StreamWriter записывает символы в потоки, используя кодировку для преобразования символов в байты.

StringReader считывает символы из строк. StringReader позволяет интерпретировать строки одним и тем же API-интерфейсом, поэтому в качестве выходных данных может использоваться класс Stream в любой кодировке, либо класс String.

StringWriter записывает символы в строки. StringWriter позволяет интерпретировать строки одним и тем же API-интерфейсом, поэтому в качестве выходных данных может использоваться класс Stream в любой кодировке, либо класс String.

TextReader является абстрактным базовым классом для класса StringReader и класса StreamReader. В то время как реализации абстрактного класса Stream предназначены для побайтового ввода и вывода, реализации TextReader предназначены для вывода знаков в кодировке Unicode.

TextWriter является абстрактным базовым классом для класса StringWriter и класса StreamWriter. В то время как реализации абстрактного класса Stream предназначены для побайтового ввода и вывода, реализации TextWriter предназначены для ввода знаков в кодировке Unicode.

Common I/O Stream Classes

A BufferedStream is a Stream that adds buffering to another Stream such as a NetworkStream. (FileStream already has buffering internally, and a MemoryStream does not need buffering.) A BufferedStream can be composed around some types of streams in order to improve read and write performance. A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system.

A CryptoStream links data streams to cryptographic transformations. Although CryptoStream derives from Stream, it is not part of the System.IO namespace, but is in the System.Security.Cryptography namespace.

A MemoryStream is a nonbuffered stream whose encapsulated data is directly accessible in memory. This stream has no backing store and might be useful as a temporary buffer.

A NetworkStream represents a Stream over a network connection. Although NetworkStream derives from Stream, it is not part of the System.IO namespace, but is in the System.Net.Sockets namespace.

I/O and Security

When using the classes in the System.IO namespace, operating system security requirements such as access control lists (ACLs) must be satisfied for access to be allowed. This requirement is in addition to any FileIOPermission requirements.

Note:

ACLs can be managed programmatically.

Caution:

Default security policy for the Internet and an intranet does not allow access to files. Therefore, do not use the regular nonisolated storage I/O classes if you are writing code that will be downloaded over the Internet. Use Isolated Storage instead.

Caution:

When a file or network stream is opened, a security check is performed only when the stream is constructed. Therefore, be careful when handing these streams off to less-trusted code or application domains.