Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# File Input and Output

C# File Input and Output

A file is a collection of data stored on disk with a specified name and directory path. When a file is opened for reading or writing, it becomes a Stream。

Fundamentally, a stream is a sequence of bytes transmitted along a communication path. There are two main streams:Input stream and Output stream。Input streamis used to read data from a file (read operation),Output streamis used to write data to a file (write operation).

C# I/O Classes

The System.IO namespace has a variety of classes for performing various file operations, such as creating and deleting files, reading or writing files, closing files, and so on.

The following table lists some common non-abstract classes in the System.IO namespace:

I/O Classes Description
BinaryReader Reads raw data from a binary stream.
BinaryWriter Writes raw data in binary format.
BufferedStream Temporary storage of a byte stream.
Directory Helps manipulate directory structures.
DirectoryInfo Used to perform operations on directories.
DriveInfo Provides information about drives.
File Helps handle files.
FileInfo Used to perform operations on files.
FileStream Used for reading from and writing to any position in a file.
MemoryStream Used for random access to a data stream stored in memory.
Path Performs operations on path information.
StreamReader Used to read characters from a byte stream.
StreamWriter Used to write characters to a stream.
StringReader Used to read from a string buffer.
StringWriter Used to write to a string buffer.

FileStream Class

In the System.IO namespace, the FileStream class helps with reading, writing, and closing files. This class derives from the abstract class Stream.

You need to create a FileStream object to create a new file, or to open an existing file. The syntax for creating a FileStream object is as follows:



FileStream  = new FileStream( ,

, , );

For example, create a FileStream object F to read a file named sample.txt :



FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

Parameters Description
FileMode

FileMode The enumeration defines various ways to open a file. The members of the FileMode enumeration are:

  • Append: Opens an existing file and places the cursor at the end of the file. If the file does not exist, the file is created.
  • Create: Creates a new file. If the file already exists, the old file is deleted and a new one is created.
  • CreateNew: Specifies that the operating system should create a new file. If the file already exists, an exception is thrown.
  • Open: Opens an existing file. If the file does not exist, an exception is thrown.
  • OpenOrCreate: Specifies that the operating system should open an existing file. If the file does not exist, a new file with the specified name is created and opened.
  • Truncate: Opens an existing file; once opened, the file is truncated to zero bytes. We can then write entirely new data to the file, but the file's original creation date is retained. If the file does not exist, an exception is thrown.
  • | | FileAccess |

    FileAccess The members of the enumeration are:Read、ReadWrite and Write。

    | | FileShare |

    FileShare The members of the enumeration are:

    |

    Example

    The following program demonstrates the use of the FileStream class:

    Example

    using

    System

    ;

    using

    System.IO

    ;

    namespace

    FileIOApplication

    {

       

    class

    Program

       

    {

           

    static

    void

    Main

    (

    string

    [

    ]

    args

    )

           

    {

                FileStream F

    =

    new

    FileStream

    (

    "test.dat"

    ,

                FileMode

    .

    OpenOrCreate

    , FileAccess

    .

    ReadWrite

    )

    ;

               

    for

    (

    int

    i

    =

    1

    ;

    i

    <=

    20

    ;

    i

    ++

    )

               

    {

                    F

    .

    WriteByte

    (

    (

    byte

    )

    i

    )

    ;

               

    }

                F

    .

    Position

    =

    0

    ;

               

    for

    (

    int

    i

    =

    0

    ;

    i

    <=

    20

    ;

    i

    ++

    )

               

    {

                    Console

    .

    Write

    (

    F

    .

    ReadByte

    (

    )

    " "

    )

    ;

               

    }

                F

    .

    Close

    (

    )

    ;

                Console

    .

    ReadKey

    (

    )

    ;

           

    }

       

    }

    }

    When the code above is compiled and executed, it produces the following result:

    
    
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
    
    

    C# Advanced File Operations

    The example above demonstrates simple file operations in C#. However, to fully leverage the power of the C# System.IO classes, you need to know the properties and methods commonly used by these classes.

    In the following sections, we will discuss these classes and the operations they perform. Click the links to learn more about each section:

    Topic Description
    Reading and Writing Text Files It involves reading and writing text files.StreamReader and StreamWriter class helps with reading and writing text files.
    Reading and Writing Binary Files It involves reading and writing binary files.BinaryReader and BinaryWriter class helps with reading and writing binary files.
    Windows File System Operations It enables C# programmers to browse and locate Windows files and directories.