Topic : File Handling in C# Programming Language - Sequential & Random Access File | File Input/Output



Introduction to File System:

image: File Operation

Console Application: Console Application means an Application that has a text-based interface. (black screen window)

Until Now we are using Console Oriented Input/output System. so using Console Application, When a Program is terminated, the Program data stored in main memory is lost. For Small Program like calculator, we don't need to store data permanently. but what's about some large programs, where we need to display result of Students, or calculate the Monthly salary of employee, and we need to save the result for further usage.

Whenever we create a Program, and enter any data to process, after the program is terminated, the data stored in the memory will lost. so to store our data Permanent, C# provide File Handling. This File Handling Provides a Mechanism to store output of a program in a file and read from a file on the disk.

So far, we have been using System Namespace which provide some methods like ReadLine() and WriteLine() to take input console Application and Display output to a Console Respectively. Now, We are going to Introduce one more namesapce Systme.IO to write into the file and read from the file.

File and Stream:

A File is a collection of Data stored on a disk with specific name, extension and Directory path. When we Open File Using C# for Reading or Writing Purpose, it becomes Stream. The stream is the Sequence of Bytes passing through the Communication path. There are two main Stream.

  • » Input Stream: The Input Stream is Used for Reading Data from file.
    » Output Stream: The Output Stream is used for Writing Data into The File.



As A C# Programmer, Several Times We need to Save Information on a Disk. We will not get Database everywhere to save Information and Our Project may require saving information in a Txt file, doc file, pdf file, or any other file types. So we Must know the Concept of File Handling.



Implementing the File I/O Operations - System.IO:

The System.IO Namespace Provide us various class to Handle the File Operation. We can use this namespace To Performing Various Tasks with file, such as creating and Deleting files, Reading From or Writing to File, Closing File, Updating file, etc. Before Moving into details, let us have a look at the commonly used class of the System.IO Namespace.



Class Name:Description:
BinaryReaderReads primitive data from a binary stream.
BinaryWriterWrites primitive data in binary format.
BufferedStreamA temporary storage for a stream of bytes.
DirectoryHelps in manipulating a directory structure.
DirectoryInfoUsed for performing operations on directories.
DriveInfoProvides information for the drives.
FileHelps in manipulating files.
FileInfoUsed for performing operations on files.
FileStreamUsed to read from and write to any location in a file.
MemoryStreamUsed for random access to streamed data stored in memory.
PathPerforms operations on path information.
StreamReaderUsed for reading characters from a byte stream.
StreamWriterIs used for writing characters to a stream.
StringReaderIs used for reading from a string buffer.
StringWriterIs used for writing into a string buffer.


The FileStream Class:

The FileStream Class in the System.IO Namespace helps in Reading from the file, writing to the file and closing the files. To Open a File or Create a New File, We need to Create a FileStream Object. The basic Syntax is:


FileStream Object_Name = New FileStream(<File_name>,  <FileMode Enumerator>,    <FileAccess Enumerator>,   <FileShare Enumerator>);

  • In Above Syntax:

    » The FileMode Enumerator Defines Various Methods of Opening files. The Members of the FileMode Enumerator are:

    MethodDescription
    AppendIt opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.
    CreateIt creates a new file.
    CreateNewIt specifies to the operating system, that it should create a new file.
    OpenIt opens an existing file.
    OpenOrCreateIt specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.
    TruncateIt opens an existing file and truncates its size to zero bytes.


    » The FileAccess Enumerator define some members to read from the file, or write into the file:

    MethodDescription
    ReadAllow opening of a File for Reading Purpose only.
    WriteAllow Opening of a File for Writing Purpose only.
    ReadWriteAllow Opening of a File for both Reading and Writing Purpose.


    » The FileShare Enumerator Contain Some members for controlling the Sharing of a File by other FileStream. it Typically sets whether two different Application can Simultaneously read from same file. the members are:

    InheritableIt allows a file handle to pass inheritance to the child processes
    NoneIt declines sharing of the current file
    ReadIt allows opening the file for reading
    ReadWriteIt allows opening the file for reading and writing
    WriteIt allows opening the file for writing



Type of File Access:



Our Application determines the method we should choose. The access mode of a file determines how we read, write, change, and delete data from the file. Some of our files can be accessed in both ways, Sequentially and randomly as long as our Programs are written properly and the data lends itself to both types of file access.



Sequential Access File:

A Sequential file has to be Accessed in the same order the file was written. Let's take an example of Cassette Tapes: We play music in the same order it was recorded. we can Quickly fast-forward or rewind over songs we don't want to listen to, but the order of the songs dictates what we do to play the song we want. But it is difficult, and sometimes impossible, to insert data in the middle of two other songs on a tape. The Only way to truly add or delete records from the middle of a Sequential file is to create a completely New file that combines both old and new records.




It Might seem that Sequential files are limiting, but it turns out that many applications lend themselves to Sequential-file processing.


Random Access File:

Unlike Sequential files, we can access Random Access files in any order we want. Think of data in a Random Access file as we would songs on a compact disc or record, we can go directly to any song we want without having to play or fast-forward over the other songs. If we want to play the first song, the sixth song, and then the fourth song, we can do so. The order of play has nothing to do with the order in which the songs were originally recorded. Random-file access sometimes takes more Programming but rewards our effort with a more flexible file-access method.




















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement