Streams

Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. The Stream class and its derived classes provide a generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices.

Depending on the underlying data source or repository, streams might support only some of these capabilities. An application can query a stream for its capabilities by using the CanRead, CanWrite, and CanSeek properties.

The Read and Write methods read and write data in a variety of formats. For streams that support seeking, use the Seek and SetLength methods and the Positionand Length properties to query and modify the current position and length of a stream.

Some stream implementations perform local buffering of the underlying data to improve performance. For such streams, the Flush method can be used to clear any internal buffers and ensure that all data has been written to the underlying data source or repository.
Calling Close on a Stream flushes any buffered data, essentially calling Flush for you. Close also releases operating system resources such as file handles, network connections, or memory used for any internal buffering.

Streams involve three fundamental operations:
1. You can read from streams. Reading is the transfer of data from a stream into a data structure, such as an array of bytes.
2. You can write to streams. Writing is the transfer of data from a data structure into a stream.
3. Streams can support seeking. Seeking is the querying and modifying of the current position within a stream. Seek capability depends on the kind of backing store a stream has. For example, network streams have no unified concept of a current position, and therefore typically do not support seeking.

EXAMPLE:

Remember to include these libraries.

using System;
using System.IO;

class FileFileInfoApp
{
static void Main(string[] args)
{
FileInfo f = new FileInfo(“Bar.txt”);
FileStream fs = f.Create();

StreamWriter w = new StreamWriter(fs);
w.Write(“Hello World”);
w.Close();

fs = f.Open(FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader r = new StreamReader(fs);
string t;
while ((t = r.ReadLine()) != null){
Console.WriteLine(t);
}
w.Close();
fs.Close();
f.Delete();
}
}

This creates a new file Bar.txt containing “Hello World” string in it.

——————————————————————————————————————–

using System;
using System.IO;

public class DeleteTest
{
public static void Main()
{
// Create a reference to a file.
FileInfo fi = new FileInfo(“temp.txt”);
// Actually create the file.
FileStream fs = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
}
}

——————————————————————————————————————-

using System;
using System.IO;
using System.Text;

class Test
{

public static void Main()
{
string path = @”c:\MyTest.txt”;
FileInfo fi = new FileInfo(path);

// Delete the file if it exists.
if (fi.Exists)
{
fi.Delete();
}

//Create the file.
using (FileStream fs = fi.Create())
{
Byte[] info =
new UTF8Encoding(true).GetBytes(“This is some text in the file.”);

//Add some information to the file.
fs.Write(info, 0, info.Length);
}

//Open the stream and read it back.
using (StreamReader sr = fi.OpenText())
{
string s = “”;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}

——————————————————————————————————————-

using System;
using System.IO;
using System.Text;

class Test
{

public static void Main()
{
string path = @”c:\MyTest.txt”;
FileInfo fi = new FileInfo(path);

// Delete the file if it exists.
if (!fi.Exists)
{
//Create the file.
using (FileStream fs = fi.Create())
{
Byte[] info = new UTF8Encoding(true).GetBytes(“This is some text in the file.”);
//Add some information to the file.
fs.Write(info, 0, info.Length);
}
}

//Open the stream and read it back.
using (FileStream fs = fi.OpenRead())
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}

——————————————————————————————————————-