Breaking News

Wednesday 25 September 2013

c# FILE I/O ( Input stream and output stream )

A file is collection of data stored in a disk with specific name and a directory path. when a file is opened for reading or writing , its became a stream.

Now what is stream?

the stream is basically the sequence of bytes passing through the communication path.

there are two main stream : the input stream and the output stream.
the input stream is used for reading data from a file and the output stream is used for write into the file.

C# I/O Classes

The system.IO namespace have various classes that are used for performing various operation with file , like creating and deleting file , reading from and writing into a file , closing a file etc.

some commonly used non-abstract classes in the system.IO namespace:

  • Binary Reader          >>  reads primitive data from a binary stream.
  • Binary Writer           >>  writes primitive data in binary format.
  • Buffered Stream       >>  a temporary storage for a stream of bytes.
  • Directory                  >>  helps in manipulating a directory structure.
  • Directory Info          >>  used for performing operation on directories.
  • Drive Info                >>  provides information for the drive.
  • File                          >>  helps in manipulating files.
  • File Info                  >>  used for performing operation on files.
  • File Stream             >>  used to read from and write to any location in a file.
  • Memory Stream     >>  used for random access to streamed data stored in memory.
  • path                       >>  performs operation on paths information.
  • Stream Reader       >>  used for reading character from a byte stream.
  • Stream Writer       >>  is used to write character to a stream.
  • String Reader       >>  is used for reading from a string buffer.
the FileStream class :

the FileStream class in the system.IO namespace helps in reading from , writing to and closing files. this class derived from the abstract class Stream.

you need to create a FileStream object  to create a new file or open an existing file. 

syntax:

FileStream  <object name> = new FileStream( <file name>, <FileMode Enumerator>, < FileAccess Enumerator>, <FileShare Enumerator>);

for example :

FileStream fs = new FileStream ("sample.txt", FileMode.Open, FileAccess. Read, FileShare. Read);

  • FileMode >> the FileMode Enumerator define various method for opening files. the                                 member of the file mode enumerator are :
                                             Append           >> it open an exist file and puts cursor at the end of                                                                          file,  or create a file if file does not exist.

           Create                 >> it is creates a new file.

   CreateNew          >> create a new file.

          Open                 >> it open an existing file.

                                               OpenOrCreate  >> open a file if exist , otherwise its should create                          a new file.
 
                                              Truncate           >> it is open a file and Truncates its size to zero                    bytes.
  • FileAccess >> FileAccess enumerator have member : Read , ReadWrite , Write.
  • FileShare  >> FileShare enumerator have the following members:
                                     Inheritable >> it allow a file handle to pass inheritance to the child process.

                         None >> it declines sharing of the current file.

                       Read >> it allow opening the file for reading.

                              ReadWrite >>  it allow opening a file for reading and writing.

                    Write >> it allow opening the file for writing.

example:

the following program demonstrates use of the FileStream class :

using system;
using system.IO;

namespace FileToApplication
{
    class program
      {
         static void Main(String [] args)
           {
              FileStream fs = new FileStream ( "test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

for ( int i = 1; i<=20; i++)
   { 
       fs.WriteByte ((byte)i);
    }
fs.Position = 0;

for ( int i = 0; i<=20; i++)
    {
       Console.Write( fs.ReadByte() + "");
    }
fs.Close();
Console.ReadKey();
         }
    }
}

when the above code is compiled and executed , its produces the following result :


Read more ...

how to implement File IO using FileStream Class C#

Read more ...

understanding exception handling C#

Read more ...
© 2013 Post by repter_x