File Stream in C#

Sequential-Access File

 So far we haven’t talked about writing words or sentences to a hard drive or reading from it. This part will guide you through those processes. File streams can be stored in plain text and binary format. There are two types of file streams—sequential-access file and random-access file.

Sequential-Access File

In sequential-access file, you can write data to the file or read data from it sequentially from the beginning of the file to the end and vice versa.

Writing to a sequential-access file
 

In the below example, to write text to a file sequentially, we need two objects--FileStream fs object and StreadWriter fw object. The StreamWriter fw object takes the FileStream object fs as its argument.

The first argument of the FileStream constructor is the file name and its path that you want to create, write to, to read from. The second argument is the file mode. There are four main file modes that you should know. These file modes are shown blow:

-Append appends the text to the end of a file if the file already exists. If the file doesn't exist it will be created. This file mode is used in conjunction with the write file access.

-Create creates a new file if it doesn't exist. If it exists it will be overridden.

-CreateNew creates a new file if it doesn't exist. An error will occur is the file already exists.

-OpenOrCreate opens a file if it already exists or create a new one if it doesn't exist.

The third argument of the FileStream constructor is file access. There are three main types of file access you should know:

-Read can only read data from a file.

-ReadWrite can read and write data to a file.

-Write can only write data to a file

Example:

FileStream fs;
StreamWriter fw;

//create file stream object
fs = new FileStream("D:\\test.txt", FileMode.OpenOrCreate, FileAccess.Write);
//create writer object
fw = new StreamWriter(fs);
//write text to file
fw.WriteLine("C# Programming");
fw.WriteLine("C Programming");
fw.WriteLine("C++ Programming");

//close FileStream object used in writing operations

fs.Close();

Reading from a sequential-access file

To read data from a file you need one FileStream object and one StreamReader object. The StreamReader object accepts the FileStream object as its argument.

 FileStream fs;
StreamReader fr;
//create file stream object
fs = new FileStream("D:\\test.txt", FileMode.Open , FileAccess.Read );
//create reader objec
fr=new StreamReader(fs);
string content=fr.ReadLine() ;
while (!fr.EndOfStream )
{
Console.WriteLine(content);
content = fr.ReadLine();
}
//close FileStream object
fs.Close();




HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.