Monday 11 August 2014

File I/O in C#

FILE:
A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.
The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

C# I/O Classes:

The System.IO namespace has various class that are used for performing various operation with files, like creating and deleting files, reading from or writing to a file, closing a file etc.

The following table shows some commonly used non-abstract classes in the System.IO namespace:
I/O Class
Description
BinaryReader
Reads primitive data from a binary stream.
BinaryWriter
Writes primitive data in binary format.
BufferedStream
A temporary storage for a stream of bytes.
Directory
Helps in manipulating a directory structure.
DirectoryInfo
Used for performing operations on directories.
DriveInfo
Provides information for the drives.
File
Helps in manipulating files.
FileInfo
Used for performing operations on files.
FileStream
Used to read from and write to any location in a file.
MemoryStream
Used for random access to streamed data stored in memory.
Path
Performs operations on path information.
StreamReader
Used for reading characters from a byte stream.
StreamWriter
Is used for writing characters to a stream.
StringReader
Is used for reading from a string buffer.
StringWriter
Is used for writing into 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 derives from the abstract class Stream.
You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows:

Parameter
Description
FileMode
The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are:
·         Append: It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.
·         Create: It creates a new file.
·         CreateNew: It specifies to the operating system, that it should create a new file.
·         Open: It opens an existing file.
·         OpenOrCreate: It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.
·         Truncate: It opens an existing file and truncates its size to zero bytes.
FileAccess
FileAccess enumerators have members: Read, ReadWrite and Write.
FileShare
FileShare enumerators have the following members:
·         Inheritable: It allows a file handle to pass inheritance to the child processes
·         None: It declines sharing of the current file
·         Read: It allows opening the file for reading
·         ReadWrite: It allows opening the file for reading and writing
·         Write: It allows opening the file for writing


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
              FileStreams fs = new FileStreams();
            fs.ReadWriteFile();
        }

    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class FileStreams
    {
        public void ReadWriteFile()
        {
            try
            {
                FileStream fs = new FileStream("D:\\test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                for (int i = 0; i <= 20; i++)
                {
                    fs.WriteByte((byte)i);
                }
                fs.Position = 0;
                for (int i = 0; i <= 20; i++)
                {
                    Console.WriteLine(fs.ReadByte() + " ");
                }
                fs.Close();
                Console.ReadLine();
            }
            catch (FileNotFoundException f)
            {
                throw f;
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
            finally
            {

            }
        }
    }
}


No comments:

Post a Comment