You are on page 1of 19

File Handling

Streams
streams allow a programmer to layer different
stream classes to provide functionality
C# handles streams is combining Input and Output.
Stream is an abstract class from which other
stream class are derived

Streams involve three fundamental operations .


we can read from streams.
we can write to streams..
Streams can support seeking.

streams :
The system.IO Namespace :

Stream
BinaryReader
BinaryWriter
BufferedStream
FileStream
MemoryStream

NetworkStream
StreamReader
StreamWriter
StringReader
TextReader
TextWriter

stream methods and properties :

BeginRead()
BeginWrite()
Close()
EndRead()
EndWrite()
Flush()
Read()
ReadByte()
Seek()

SetLength()
Write()
WriteByte()
CanRead
CanWrite
CanSeek
Length
Position

BinaryReader :
Reads binary data from a stream.
BinaryWriter

Writes binary data to a stream.

using System;
class BinReadWriteApp
{
static void Main(string[] args)
{
Stream s = new FileStream("Foo.txt", FileMode.Create);
StreamWriter w = new StreamWriter(s);
w.Write("Hello World ");
w.Write(123);
w.Write(' ');
w.Write(45.67);
w.Close();
s.Close();

Stream t = new FileStream("Bar.dat", FileMode.Create);


BinaryWriter b = new BinaryWriter(t);
b.Write("Hello World ");
b.Write(123);
b.Write(' ');
b.Write(45.67);
b.Close();
t.Close();
}
}

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

using System;
class MemStreamApp
{
static void Main(string[] args)
{
MemoryStream m = new MemoryStream(64);
Console.WriteLine("Length: {0}\tPosition: {1}\tCapacity: {2}",
m.Length, m.Position, m.Capacity);
for (int i = 0; i < 64; i++)
{
m.WriteByte((byte)i);
}

Console.WriteLine("Length: {0}\tPosition: {1}\tCapacity: {2}",


m.Length, m.Position, m.Capacity);
Console.WriteLine("\nContents:");
byte[] ba = m.GetBuffer();
foreach (byte b in ba)
{
Console.Write("{0,-3}", b);
}
m.Close();
}
}

StreamReader :
Reads characters from a Stream, using Encoding to
convert characters to and from bytes.

StreamWriter :
Writes characters to a Stream, using Encoding
to convert characters to bytes.

using System;
public class ReadWriteApp
{
public static void Main(string[] args)
{
FileStream s = new FileStream("Bar.txt",
FileMode.Create);
StreamWriter w = new StreamWriter(s);
w.Write("Hello World");
w.Close();

s = new FileStream("Bar.txt", FileMode.Open);


StreamReader r = new StreamReader(s);
string t;
while ((t = r.ReadLine()) != null)
{
Console.WriteLine(t);
}
w.Close();
}
}

StringReader :
Reads characters from a String. StringReader allows
you to treat a String with the same API; thus, your
output can be either a Stream in any encoding or a
String.

StringWriter :
Writes characters to a String. StringWriter allows you to
treat a String with the same API; thus, your output can
be either a Stream in any encoding or a String.

using System;
class StringReadWriteApp

{
static void Main(string[] args)
{
StringWriter w = new StringWriter();
w.WriteLine("Sing a song of {0} pence", 6);
string s = "A pocket full of rye";

w.Write(s);
w.Write(w.NewLine);
w.Write(String.Format(4 +" and " +20 +"
blackbirds"));
w.Write(new StringBuilder(" baked in a pie"));
w.WriteLine();
w.Close();
Console.WriteLine(w);
}
}

TextReader :
The abstract base class for StreamReader and StringReader objects.
While the implementations of the abstract Stream class are
designed for byte input and output, the implementations of
TextReader are designed for Unicode character output.

TextWriter :
The abstract base class for StreamWriter and StringWriter objects.
While the implementations of the abstract Stream class are
designed for byte input and output, the implementations of
TextWriter are designed for Unicode character input.

FileStreams :
In addition to basic Stream behavior, this class supports random
access to files through its Seek method and supports both
synchronous and asynchronous operation.

BufferedStream
A Stream that adds buffering to another Stream, such as a
NetworkStream. (FileStream already has buffering internally, and a
MemoryStream doesn't need buffering.) A BufferedStream object
can be composed around some types of streams to improve read
and write performance.

You might also like