C# FileInfo OpenWrite

Description

FileInfo OpenWrite Creates a write-only FileStream.

Syntax

FileInfo.OpenWrite has the following syntax.


public FileStream OpenWrite()

Returns

FileInfo.OpenWrite method returns A write-only unshared FileStream object for a new or existing file.

Example

The following example opens a file for writing and then reads from the file.


/* w ww .  j  a va2 s.  com*/
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        using (FileStream fs = fi.OpenWrite()) 
        {
            Byte[] info =  new UTF8Encoding(true).GetBytes("This is from java2s.com.");
            fs.Write(info, 0, info.Length);
        }
        using (FileStream fs = fi.OpenRead()) 
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b,0,b.Length) > 0) 
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




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