19
File Handling

Session 6

Embed Size (px)

DESCRIPTION

sixth

Citation preview

Page 1: Session 6

File Handling

Page 2: Session 6

• 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

Page 3: Session 6

• Streams involve three fundamental operations .

– we can read from streams.

– we can write to streams..

– Streams can support seeking.

Page 4: Session 6

streams :

The system.IO Namespace :

– Stream – BinaryReader– BinaryWriter– BufferedStream– FileStream– MemoryStream

– NetworkStream– StreamReader– StreamWriter– StringReader– TextReader– TextWriter

Page 5: Session 6

stream methods and properties :

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

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

Page 6: Session 6

• BinaryReader :

–Reads binary data from a stream.

• BinaryWriter

– Writes binary data to a stream.

Page 7: Session 6

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();

Page 8: Session 6

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();

}}

Page 9: Session 6

• 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.

Page 10: Session 6

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); }

Page 11: Session 6

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(); }}

Page 12: Session 6

• 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.

Page 13: Session 6

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();

Page 14: Session 6

s = new FileStream("Bar.txt", FileMode.Open);StreamReader r = new StreamReader(s);string t;

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

Page 15: Session 6

• 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.

Page 16: Session 6

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";

Page 17: Session 6

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); }}

Page 18: Session 6

• 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.

Page 19: Session 6

• 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.