C# tutorial-File Stream: Sequential-Access File

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:

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.