Stream
class and its derived classes provide a generic view of these different types of input/output and isolate the programmer from the specific details of the OS.
Stream
is the abstract base class of all streams, which is an abstraction of a sequence of bytes.https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0de90720-cf53-4c4b-be4b-d880a81b5e5d/Untitled
var originalInstance = new MyObject();
IFormatter formatter = new BinaryFormatter();
// Serialization
byte[] bytes;
using (var writeStream = new MemoryStream())
{
formatter.Serialize(writeStream, originalInstance);
bytes = writeStream.ToArray();
}
// Deserialization
using (var readStream = new MemoryStream(bytes))
{
var deserializedInstance = (MyObject) formatter.Deserialize(readStream);
}
System.Text.Encoding
is the class that helps you convert between bytes and strings.FileStream
ClassFileStream
class in the System.IO namespace helps in reading from, writing to and closing files.FileStream
object to create a new file or open an existing file.FileStream
, you can use the synchronous method Write, which expects a byte array.
const string filePath = "MyTest.txt";
using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
// Write into the file stream.
var bytesToWrite = Encoding.UTF8.GetBytes("Test String...");
fileStream.Write(bytesToWrite, 0, bytesToWrite.Length);
}
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Read the written data from the file stream.
var bytesBuffer = new byte[fileStream.Length];
fileStream.Seek(0, SeekOrigin.Begin);
var bytesToRead = bytesBuffer.Length;
var bytesRead = 0;
while (bytesToRead > 0)
{
var resultedBytes = fileStream.Read(bytesBuffer, bytesRead, bytesToRead);
if (resultedBytes == 0) // No bytes remained to read.
bytesToRead = 0;
else
{
bytesRead += resultedBytes;
bytesToRead -= resultedBytes;
}
}
var outputString = Encoding.UTF8.GetString(bytesBuffer);
}
File
Class UtilitiesFile
class supports a CreateText()
method that creates a file with an UTF-8 encoding for you.StreamWriter
, a class that inherits from TextWriter
and enables you to directly write characters to a Stream with a particular encoding.StreamReader
to read a text file. It uses a default encoding and returns the bytes to as a string.const string filePath = "MyTest.txt";
using (FileStream fileStream = File.OpenRead(filePath))
{
var bytesBuffer = new byte[fileStream.Length];
for (var i = 0; i < fileStream.Length; i++)
bytesBuffer[i] = (byte)fileStream.ReadByte();
}
using (StreamReader streamReader = File.OpenText(filePath))
{
var fileContent = streamReader.ReadToEnd();
}
// You can use this approach to read the file content line by line.
using (StreamReader streamReader = File.OpenText(filePath))
{
while (!streamReader.EndOfStream)
{
var lineContent = streamReader.ReadLine();
}
}
using (StreamWriter streamWriter = File.CreateText(filePath))
{
streamWriter.WriteLine("Line 1");
streamWriter.Write("Line 2");
}
GZipStream
, which takes another Stream
object in its constructor and use it as the input or output for the compression.