Introduction to File System:
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.
“
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: |
---|---|
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 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>);
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.