C# FileStream FileStream(String, FileMode)

Description

FileStream FileStream(String, FileMode) Initializes a new instance of the FileStream class with the specified path and creation mode.

Syntax

FileStream.FileStream(String, FileMode) has the following syntax.


public FileStream(
  string path,
  FileMode mode
)

Parameters

FileStream.FileStream(String, FileMode) has the following parameters.

  • path - A relative or absolute path for the file that the current FileStream object will encapsulate.
  • mode - A constant that determines how to open or create the file.

Example

The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.


using System;//w w w.  ja va  2  s . com
using System.IO;

class FStream
{
    static void Main()
    {
        const string fileName = "Test.dat";
        byte[] dataArray = new byte[100];
        new Random().NextBytes(dataArray);

        using(FileStream fileStream = new FileStream(fileName, FileMode.Create))
        {
            for(int i = 0; i < dataArray.Length; i++)
            {
                fileStream.WriteByte(dataArray[i]);
            }
            fileStream.Seek(0, SeekOrigin.Begin);
            for(int i = 0; i < fileStream.Length; i++)
            {
                Console.WriteLine(fileStream.ReadByte());
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter